newLisp doesn't have long long support?

Started by _ex_, August 23, 2006, 09:05:22 AM

Previous topic - Next topic

_ex_

hi again I'm trying to obtain the value of 2 to the exponent 38

this is basic question that can be solved beautifully as:

print( 2 ** 38 ); PERL

puts( 2 ** 38 ) RUBY

print( 2 ^ 38 ) LUA

or as uglier as

System.out.println( Math.round( Math.pow( 2, 38 ) ) ); JAVA

Lutz

#1
http://newlisp.org/downloads/newlisp_manual.html#pow">http://newlisp.org/downloads/newlisp_manual.html#pow



and see also here for quick overview of functions available in newLISP in differewnt groups:



http://newlisp.org/downloads/newlisp_manual.html#functions">http://newlisp.org/downloads/newlisp_ma ... #functions">http://newlisp.org/downloads/newlisp_manual.html#functions



Lutz

_ex_

#2
Of course i'm trying:

(println (floor (pow 2 38)))

2.748779069e+011

Not the exact answer:

274877906944

cormullion

#3
(set 'a-large-number (pow 2 38))
(set 'an-even-larger-number (add 1 a-large-number))
(println (format "%20.f" a-large-number) (format "%20.f" an-even-larger-number))(println (format "%20.f" (mul a-large-number an-even-larger-number)))

Lutz

#4
The next development version 8.9.7 to be released this Friday or Saturday has 64 bit long long support for all integer operations with 19 digits of precision (currently in test). Floats are double 64 bit already.



But as Corumullion has shown, your problem is formatting. If you print without formatting newLISP will choose the "%g" format and give you 9 digits of precision. When formatting right you will have up to 15/16 digits of precision (52bits)  when using 64 bit double floats.



Lutz

Lutz

#5
... you should also read the chapter:

http://newlisp.org/downloads/newlisp_manual.html#int_float">http://newlisp.org/downloads/newlisp_ma ... #int_float">http://newlisp.org/downloads/newlisp_manual.html#int_float



Lutz

_ex_

#6
thank you guys! :)

I did this for this problem in particular:

[DON'T LAUGH I'M A NEWBIE....]
(define (double strNumber)

(setq carry 0)
(setq strDouble "")
(reverse strNumber)
(for (k 0 (- (length strNumber) 1))

(setq d (- (char strNumber k) (char "0")))
(setq m (+ (* d 2) carry))
(setq md (% m 10))
(setq strDouble (string md strDouble))
(setq carry (/ m 10))
)
(if (= carry 1)
(setq strDouble (string carry strDouble))
)
(setq return strDouble)
)

(define (pow2 n)

(setq strNumber "1")
(for (k 1 n)
(setq strNumber (double strNumber))
)
(setq return strNumber)
)

(println (pow2 38))

(exit)
[DON'T LAUGHT]

newdep

#7
We never laugh at newby's !! every solution is a working one!! ;-) thats also the power of newlisp !



Welcome btw ;-)
-- (define? (Cornflakes))

Lutz

#8
... but just in case you need a shorter solution ;)


(define (pow2 n)
    (trim (format "%16.f" (pow 2 n))))

(pow2 38) => "274877906944"

(pow2 52) => "4503599627370496"


The last shows the maximum resolution you can have with double floats in newLISP. The 'trim' strips the spaces in front for numbers less than 16 digits.



Lutz



ps: note that in the upcoming development version (pow x) will default to (pow x 2) as suggested by Jeremy Dunn