Page 1 of 1

A strange problem. Quite confusing

Posted: Wed Apr 30, 2014 9:38 pm
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 ; )

Re: A strange problem. Quite confusing

Posted: Wed Apr 30, 2014 10:46 pm
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...

Re: A strange problem. Quite confusing

Posted: Wed Apr 30, 2014 10:53 pm
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.

Re: A strange problem. Quite confusing

Posted: Thu May 01, 2014 8:19 am
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

Re: A strange problem. Quite confusing

Posted: Thu May 01, 2014 8:36 am
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! :)

Re: A strange problem. Quite confusing

Posted: Thu May 01, 2014 9:09 am
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.

Re: A strange problem. Quite confusing

Posted: Fri May 02, 2014 8:48 pm
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.