[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
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
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
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,
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
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
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
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.
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
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
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
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
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
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
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
15 matches
Mail list logo