A strange problem. Quite confusing

For the Compleat Fan
Locked
Linux_x189
Posts: 4
Joined: Wed Apr 30, 2014 9:23 pm

A strange problem. Quite confusing

Post by 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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: A strange problem. Quite confusing

Post by 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...

Linux_x189
Posts: 4
Joined: Wed Apr 30, 2014 9:23 pm

Re: A strange problem. Quite confusing

Post by Linux_x189 »

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...
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
Posts: 4
Joined: Wed Apr 30, 2014 9:23 pm

Re: A strange problem. Quite confusing

Post by Linux_x189 »

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: A strange problem. Quite confusing

Post by 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/Introducti ... th_numbers, please do! :)

Linux_x189
Posts: 4
Joined: Wed Apr 30, 2014 9:23 pm

Re: A strange problem. Quite confusing

Post by Linux_x189 »

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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: A strange problem. Quite confusing

Post by Lutz »

Code: Select all

(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.

Locked