[Jprogramming] Converting character arrays to extended integers
Elijah Stone
elronnd at elronnd.net
Sun Apr 24 01:04:15 UTC 2022
x:".s first computes ".s, which is a floating-point approximation of the
number represented by s, and _then_ converts that floating-point approximation
to an extended precision value. (The latter conversion may also be
approximate, pursuant to comparison tolerance, but is always exact when
applied to floating-point values with no fractional part, as is the case
here.)
It is the same as if you had written x:34823742834835345345345346546454237;
the number is approximated before x: has a chance to get at it.
On Sun, 24 Apr 2022, Thomas Bulka wrote:
> Hi Raul,
>
> alas, you are right. Say, I have the following string:
>
> s =: '34823742834835345345345346546454237'
>
> Since x: does not accept strings as arguments I first tried to convert
> s into an integer before casting it to extended int, as you showed
> before. However, this gives me the wrong result, while my first
> approach yields the correct result.
>
> x:@:". s NB. 34823742834835345123208419344384000
> ".@:,&'x' s NB. 34823742834835345345345346546454237
>
> I don't quite understand the behaviour of the first line, though.
>
> Regards,
>
> Thomas
>
> Am So., 24. Apr. 2022 um 01:51 Uhr schrieb Raul Miller <rauldmiller at gmail.com>:
>>
>> I do not think you get the same results using x:
>>
>> I think your ".@,&'x'@> data was fine.
>>
>> You could use 10#.x:".@>@> data if you don't mind the speed penalty
>> and excessive use of memory, but x:".@> data does not give the same
>> answer, and as far as I know, x: does not accept string arguments.
>>
>> Am I missing something?
>>
>> Thanks,
>>
>> --
>> Raul
>>
>> On Sat, Apr 23, 2022 at 6:46 PM Thomas Bulka
>> <thomas.bulka at constraintegic.net> wrote:
>> >
>> > Sorry for the noise,
>> >
>> > question can be revoked, since I found x: ...
>> >
>> > Regards,
>> >
>> > Thomas
>> >
>> > Am Sa., 23. Apr. 2022 um 23:54 Uhr schrieb Thomas Bulka
>> > <thomas.bulka at constraintegic.net>:
>> > >
>> > > Hello everyone,
>> > >
>> > > say, I have a boxed list of strings which represent large integers
>> > > (originally I imported the data from a text file):
>> > >
>> > > data =: '34823742834835345345345346546454237';'34593823489289342893498945349539'
>> > >
>> > > I want to convert the character data into integers to use them for
>> > > computations, but ". gives me non-extended integers, so:
>> > >
>> > > ".@> data NB. 3.48237e34 3.45938e31
>> > >
>> > > I know, of course, that I can construct an extended integer by means
>> > > of adding a trailing x to an integer literal. I wonder, however, how
>> > > this can be done automatically. I managed to get the result I want by
>> > > adding the trailing 'x' to the character arrays before using ". on
>> > > them:
>> > >
>> > > ".@,&'x'@> data NB. Works as desired
>> > >
>> > > However, intuitively this seems to be a quite strange solution and I
>> > > hope there is a verb which can be used to explicitly convert the
>> > > character strings into extended integers. Unfortunately, I've not been
>> > > able to find it.
>> > >
>> > > Any suggestions on this?
>> > >
>> > > Regards,
>> > >
>> > > Thomas
>> > ----------------------------------------------------------------------
>> > 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
More information about the Programming
mailing list