For examplex: 987654321*987654321fy: string xy[1]"."y is stored in scientific notation. Is it possible to store everyactual digit of x in y?
http://code.kx.com/wiki/Reference/DisplayPrecisionModify the display precision - default is 7q)x:761*9600540f ;x7.306011e+09q)\P 20q)x:761*9600540f ;x7306010940fRgds,NickOn Nov 9, 8:27?pm, Sean Larsen <sean.larse…> wrote:> For example>> x: 987654321*987654321f> y: string x> y[1]>> “.”>> y is stored in scientific notation. ?Is it possible to store every> actual digit of x in y?</sean.larse…>
No. like most programming languages (including C, C#, Python, Ruby, Pascal and friends), core k/q uses a floating point representation, which has ~16 digits of precision (specifically, 64 bit IEEE 754 “double precision”). While you can change the printing precision with \P, internally, only ~16 digits are stored.
Read http://en.wikipedia.org/wiki/Double\_precision , and http://www.validlab.com/goldberg/paper.pdf for the intimate details. If you don’t want to go there, just be aware that: 0.9-0.3*3 is not 0 (in any of the above languages), but rather 1.11e-16 - if you can’t ignore that in your program, you should definitely read the attached links.
To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1084)
> x: 987654321*987654321f
> y: string x
> y[1]
>
> “.”
>
> y is stored in scientific notation. Is it possible to store every
> actual digit of x in y?
in general no, q has only machine numeric types (int long real float), =
no bignum=
Thank you everyone for the replies. Since there is no inbuilt supportfor bignum I have tried to roll my own using lists and customfunctions. Unfortunately I’m stuck on addition. Using the old ‘pen andpaper’ method I would add as follows:888 +999Step 1: 17 17 17Step 2: 17 18 7 (carry column 3 to column 2)Step 3: 18 8 7 (carry column 2 to column 1)Which gives the answer of 1887. In code:x: 8 8 8y: 9 9 9sep:{(floor x%10;x mod 10)}carry:{(sep[1];y+sep[0])}Step 1: z: x+yStep 2: reverse carry[z[1];z[2]]Step 3: reverse carry[8;18]It might be a bit clumsy however all the right answers are in there. Ijust can’t figure out how to tie everything together.I figured something like(carry)zWould work. It doesn’t quite give the correct response. What am Idoing wrong?On Nov 12, 3:21?am, Aaron Davies <aaron.dav…> wrote:> > x: 987654321*987654321f> > y: string x> > y[1]>> > “.”>> > y is stored in scientific notation. ?Is it possible to store every> > actual digit of x in y?>> in general no, q has only machine numeric types (int long real float), no bignum</aaron.dav…>
i’ve played with this once
here is what i got - not too elegant or fast
numbers are in reverse order
e:{0^y til x} /extend
t:{neg[(reverse x=0)?0b]_x:x mod 10} /trim
c:{y+x div 10}[0;] /carry
a:{t c e[n;x]+e[n:1+count|count y;y]} /add
m:{(a)over(t c@)eacheach") y*neg[til count y]rotate:e[1+count+count y;x]} /multiply
s:{raze string`int$reverse x} /string
q)a[1 2 3;3 4 9]
4 6 2 1
q)321+943
1264
q)m[1 2 3;3 4 9]
3 0 7 2 0 3
q)321*943
302703
Cheers,
Attila
Thanks Attila I guess this was not the beginner problem I thought itwould be!On Nov 16, 1:42?am, Attila Vrabecz <attila.vrab…> wrote:> i’ve played with this once> here is what i got - not too elegant or fast> numbers are in reverse order>> e:{0^y til x} /extend> t:{neg[(reverse x=0)?0b]_x:x mod 10} /trim> c:{y+x div 10}[0;] /carry> a:{t c e[n;x]+e[n:1+count|count y;y]} /add> m:{(a)over(t c@)each yneg[til count y]rotate:e[1+count[x]+count y;x]} /multiply> s:{raze string`int$reverse x} /string>> q)a[1 2 3;3 4 9]> 4 6 2 1> q)321+943> 1264> q)m[1 2 3;3 4 9]> 3 0 7 2 0 3> q)321943> 302703>> Cheers,> ?Attila>> On 14 Nov 2011, at 01:03, Sean Larsen wrote:>>>>>> > Thank you everyone for the replies. Since there is no inbuilt support> > for bignum I have tried to roll my own using lists and custom> > functions. Unfortunately I’m stuck on addition. Using the old ‘pen and> > paper’ method I would add as follows:>> > 888 +> > 999>> > Step 1: 17 17 17> > Step 2: 17 18 7 (carry column 3 to column 2)> > Step 3: 18 8 7 (carry column 2 to column 1)>> > Which gives the answer of 1887. In code:>> > x: 8 8 8> > y: 9 9 9> > sep:{(floor x%10;x mod 10)}> > carry:{(sep[1];y+sep[0])}>> > Step 1: z: x+y> > Step 2: reverse carry[z[1];z[2]]> > Step 3: reverse carry[8;18]>> > It might be a bit clumsy however all the right answers are in there. I> > just can’t figure out how to tie everything together.>> > I figured something like>> > (carry)z>> > Would work. It doesn’t quite give the correct response. What am I> > doing wrong?>> > On Nov 12, 3:21 am, Aaron Davies <aaron.dav…> wrote:> >>> x: 987654321*987654321f> >>> y: string x> >>> y[1]>> >>> “.”>> >>> y is stored in scientific notation. ?Is it possible to store every> >>> actual digit of x in y?>> >> in general no, q has only machine numeric types (int long real float), no bignum>> > –> >
Submitted via Google Groups</aaron.dav…></attila.vrab…>