A strange problem. Quite confusing

Started by Linux_x189, April 30, 2014, 02:38:37 PM

Previous topic - Next topic

Linux_x189

Hi guys!

I am new to newlisp, and recently I encounting a really strange problem.



I type the following code in newlisp:

(mod (bigint (pow 9 17)) 17)



the answers  ( I typed it in windows xp(32bit) and Archlinux(64bit)) is:

10 (winxp)

8   (Archlinux)



but the answer should be 9.



And the 'pow' part is also quiet strange:

(bigint (pow 9 17))  returns 16677181699666570L

but the answer should be one less than that.



So, what is wrong I did. Or is it a bug?

Need some help ; )

cormullion

#1
I don't think pow is big-integer friendly - has (pow 9 17) already exceeded 64-bit integer range and converted to float?



The addition of bigint into newLISP is very recent, and at the moment I don't think all arithmetic operations can handle them. newLISP isn't designed to be as maths-aware a language as some others, and you'll need to be careful how you switch between int, float, and bigint...

Linux_x189

#2
Quote from: "cormullion"I don't think pow is big-integer friendly - has (pow 9 17) already exceeded 64-bit integer range and converted to float?



The addition of bigint into newLISP is very recent, and at the moment I don't think all arithmetic operations can handle them. newLISP isn't designed to be as maths-aware a language as some others, and you'll need to be careful how you switch between int, float, and bigint...


Newlisp does not have an Integer version of "pow" . (Theris is * for mul , / for div and so on...)

Ok, It looks like I should be careful of 'pow' when dealing with big numbers.

the length of (pow 9 17) is just 17 , not get out of the 64bit's limitation.

Linux_x189

#3
I think something may be wrong in newlisp.

I write my pow Integer-version of "pow" :

(define (** x y , (.pow 1)) (dotimes (i y) (set '.pow (* .pow x))))

and It just works fine with 9^17:

(** 9 17) returns:

16677181699666569



It's fine. So I continued:

(mod (** 9 17) 17) returns

8

It's wrong again!  



Now, I write my own mod funciton:

(define (mymod x y) (- x (* y (/ x y))))



(mymod (** 9 17) 17)



finally I get the right answer:

9



I think maybe some thing wrong with the "mod" function.

(** 9 17) is not a too big integer but "mod" failed to work

cormullion

#4
Don't forget to use + - * /  % rather than add sub mul div mod for integers...



If you find any errors or can add suggestions http://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers">//http://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers, please do! :)

Linux_x189

#5
Quote from: "cormullion"Don't forget to use + - * /  % rather than add sub mul div mod for integers...



If you find any errors or can add suggestions http://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers">//http://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers, please do! :)




Thx, I will check the website for some detiles.

Lutz

#6

(define (** x p)
    (let (y 1L)
        (dotimes (i p)
            (set 'y (* y x)))))

(** 10 50) => 100000000000000000000000000000000000000000000000000L


as the first value is a big int, it will propagate itself through all the calculations.