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
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
Lutz
Of course i'm trying:
(println (floor (pow 2 38)))
2.748779069e+011
Not the exact answer:
274877906944
(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)))
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
... you should also read the chapter:
http://newlisp.org/downloads/newlisp_manual.html#int_float
Lutz
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]
We never laugh at newby's !! every solution is a working one!! ;-) thats also the power of newlisp !
Welcome btw ;-)
... 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