Re: [Jprogramming] String to Rational

2021-01-06 Thread Raul Miller
[second attempt at sending -- i had sent this earlier, but apparently it never made it out.] Here's a quicky which handles arbitrary decimal fractions as rationals. It will have problems if you try it on numbers which include letters already in their representation. It may have other issues, since

Re: [Jprogramming] String to Rational

2021-01-06 Thread Justin Paston-Cooper
Thanks. That should do the job. On Wed, 6 Jan 2021 at 21:04, Devon McCormick wrote: > > I suspect you are messing around with meaningless precision if these > numbers are coming from a spreadsheet since they are already in floating > point format there but this may do what you need: > > stringToR

Re: [Jprogramming] String to Rational

2021-01-06 Thread Devon McCormick
I suspect you are messing around with meaningless precision if these numbers are coming from a spreadsheet since they are already in floating point format there but this may do what you need: stringToRational=: 3 : 0 if. 1=#tmp=. <;._1 '.',y do. tmp=. tmp,<,'0' end. ".(;tmp),'r1','0'$~#;1{tm

Re: [Jprogramming] String to Rational

2021-01-06 Thread Henry Rich
For domain errors, Look at the Dissect tool. Henry Rich On 1/6/2021 3:40 AM, Justin Paston-Cooper wrote: I should add how it works: On the right, 10^(The position of '.' in the number minus 1) On the left, remove '.' and parse as number In the middle, apply x: to both sides and divide. Also,

Re: [Jprogramming] String to Rational

2021-01-06 Thread Ric Sherlock
I don't see what you mean by "doesn't give the number you suggest". I think I am missing something. 3/50 is equal to 666.66. 333/50 is equal to 6.66. This is what I expected. Sounds like this is just a typo or copy/paste error then. Above you suggested: stringToRational '666.66' should give 3

Re: [Jprogramming] String to Rational

2021-01-06 Thread Raul Miller
The x: mechanism does first convert to floats, and then to rationals. As long as your values are not too large, the result should be exact. I imagine that it would be nice if dyadic ". or a workalike could support direct interpretation as rational numbers. FYI, -- Raul On Wed, Jan 6, 2021 at

Re: [Jprogramming] String to Rational

2021-01-06 Thread Justin Paston-Cooper
I realise that '1234.00' is not being parsed correctly. Will need to account for all zero after the decimal point. It is also wrong for numbers without a point. On Wed, 6 Jan 2021 at 12:29, Raul Miller wrote: > > I would not combine those ideas. > > Either should work alone, just fine (though you

Re: [Jprogramming] String to Rational

2021-01-06 Thread Justin Paston-Cooper
Thanks. Seems to be working now, even with a parenthesis removed from each end. I wasn't getting a syntax error. Weird. I don't see what you mean by "doesn't give the number you suggest". I think I am missing something. 3/50 is equal to 666.66. 333/50 is equal to 6.66. This is what I expected.

Re: [Jprogramming] String to Rational

2021-01-06 Thread Raul Miller
I would not combine those ideas. Either should work alone, just fine (though you need to be careful that you've always got .00 for the ones that need it, if you are removing the decimal point). FYI, -- Raul On Wed, Jan 6, 2021 at 2:20 AM Justin Paston-Cooper wrote: > > Thanks for the suggesti

Re: [Jprogramming] String to Rational

2021-01-06 Thread Ric Sherlock
Hi Justin, I'm not entirely clear what your desired behaviour/use case is, however if you add another closing parenthesis then your definition above does not give a (syntax) error, but doesn't give the number you suggest. stringToRational =: ((_&".)@(-.&'.') %&:x: 10&^@(<:@# - (i.&'.'))) stringTo

Re: [Jprogramming] String to Rational

2021-01-06 Thread Justin Paston-Cooper
I should add how it works: On the right, 10^(The position of '.' in the number minus 1) On the left, remove '.' and parse as number In the middle, apply x: to both sides and divide. Also, how can I go about actually debugging domain errors? Both sides seem to be zero-dimensional. On Wed, 6 Jan 2

Re: [Jprogramming] String to Rational

2021-01-05 Thread Justin Paston-Cooper
Thanks for the suggestion. I've combined Don's idea of using x: and your idea of removing the point and parsing that to handle all numbers of digits after the decimal point: stringToRational =: ((_&".)@(-.&'.') %&:x: 10&^@(<:@# - (i.&'.')) stringToRational '666.66' should give 333r50. Sadly this

Re: [Jprogramming] String to Rational

2021-01-05 Thread Jimmy Gauvin
IF your strings always have two numbers after the decimal point you could remove the point before converting to numeric. NB.Remove decimal point '.' -.~ each '123.45' ; '1456.12' ┌─┬──┐ │12345│145612│ └─┴──┘ NB. Convert to numeric _ ". > '.' -.~ each '123.45' ; '456.12' 12

Re: [Jprogramming] String to Rational

2021-01-05 Thread Don Guinn
x:0.66 33r50 Actually 0.66 is not exactly 0.66 internally. But it is close enough for J to make it an exact rational. However, I use pennies when handling money. That uses hardware integers. On Tue, Jan 5, 2021 at 8:43 AM Justin Paston-Cooper wrote: > Hello, > > I am reading in tab-separate

[Jprogramming] String to Rational

2021-01-05 Thread Justin Paston-Cooper
Hello, I am reading in tab-separated files with readdsv from 'tables/dsv'. Some columns consist of exact decimal numbers like 6.66. These generally involve amounts of a decimal currency. These are read in by readdsv as boxed strings. makenum from 'tables/csv' converts these strings to floats. I wo