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 ; )
A strange problem. Quite confusing
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: A strange problem. Quite confusing
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...
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...
-
- Posts: 4
- Joined: Wed Apr 30, 2014 9:23 pm
Re: A strange problem. Quite confusing
Newlisp does not have an Integer version of "pow" . (Theris is * for mul , / for div and so on...)cormullion wrote: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...
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.
-
- Posts: 4
- Joined: Wed Apr 30, 2014 9:23 pm
Re: A strange problem. Quite confusing
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
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
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: A strange problem. Quite confusing
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/Introducti ... th_numbers, please do! :)
If you find any errors or can add suggestions http://en.wikibooks.org/wiki/Introducti ... th_numbers, please do! :)
-
- Posts: 4
- Joined: Wed Apr 30, 2014 9:23 pm
Re: A strange problem. Quite confusing
cormullion wrote: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/Introducti ... th_numbers, please do! :)
Thx, I will check the website for some detiles.
Re: A strange problem. Quite confusing
Code: Select all
(define (** x p)
(let (y 1L)
(dotimes (i p)
(set 'y (* y x)))))
(** 10 50) => 100000000000000000000000000000000000000000000000000L