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. 33333/50 is equal to 666.66. 333/50 is
equal to 6.66. This is what I expected.

As far as I understand, your solution parses strings to floats, and
then converts to extended. Don says that the internal float
representation is not an exact representation of the string. This
would make sense. It may be the case that your examples correspond
with the input numbers. I would imagine that for larger numbers there
would be a discrepancy.

([: x: 0&".) '10000000000.12345' gives:

810000000010r81

This is close, but not equal to the input.

stringToRational '10000000000.12345' gives:

200000000002469r20000

This is correct.

My solution parses the digits exactly as if there were no dot and
divides by 10^(number of digits after point).

On Wed, 6 Jan 2021 at 12:20, Ric Sherlock <tikk...@gmail.com> wrote:
>
> 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.&'.')))
>
> stringToRational '666.66'
>
> 33333r50
>
> stringToRational '6.66'
>
> 333r50
>
>
> Assuming you just want to represent all strings as their equivalent
> rationals, is there a reason not to define stringToRational like this?
>
> stringToRational =: [: x: 0&".
>
> stringToRational '666.66'
>
> 33333r50
>
> stringToRational&> '66666';'6666.6';'666.66';'66.666';'6.6666';'0.66666'
>
> 66666 33333r5 33333r50 33333r500 33333r5000 33333r50000
>
>
>
> On Wed, Jan 6, 2021 at 8:20 PM Justin Paston-Cooper <paston.coo...@gmail.com>
> wrote:
>
> > 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 is giving a domain error, even though both sides of the
> > fork work alone. Could anyone suggest how to fix this?
> >
> > On Tue, 5 Jan 2021 at 19:14, Jimmy Gauvin <jimmy.gau...@gmail.com> wrote:
> > >
> > > 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'
> > > 12345 45612
> > >
> > > NB. type 4 is integer
> > >    3!:0 ] _ ". > '.' -.~  each '123.45' ; '456.12'
> > > 4
> > >
> > > On Tue, Jan 5, 2021 at 10:43 AM Justin Paston-Cooper <
> > > paston.coo...@gmail.com> wrote:
> > >
> > > > 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 would like these strings to be converted exactly to
> > > > rationals. How can I go about this in the easiest and most efficient
> > > > way? I could not find information on parsing data types from strings.
> > > >
> > > > Thanks in advance,
> > > >
> > > > Justin
> > > > ----------------------------------------------------------------------
> > > > For information about J forums see http://www.jsoftware.com/forums.htm
> > > >
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to