Ask Your Question
0

Problem with numpy integers and floats

asked 2013-10-25 15:11:00 +0200

Mohamed Gaafar gravatar image

updated 2013-10-25 15:29:40 +0200

1) 1st problem

sage: import numpy as np
sage: a = np.int64(4343678784233766587)
sage: b = np.int64(149)
sage: a*b
1572096270996914903

the answer is wrong; it should be 647208138850831221463

1) 2nd problem

sage: a = np.int64(3344773437777732743)
sage: b = np.int64(181)
sage: a*b
-3338562194645576845

the answer is negative, How Come ???

1) 3rd problem

sage: a = np.float64(3344773437777732743)
sage: b = np.float64(181)
sage: a*b
6.0540399223776962e+20  #the exact answer is 605403992237769626483

Why doesn't it give me all the numbers instead of this e*20 ???

Moreover, i just used normal int of sage (<type 'sage.rings.integer.Integer'> ) without using numpy and it works fine !!! Thank you in advance.

edit retag flag offensive close merge delete

Comments

If you use Sage and it works fine why do you switch to numpy ?

vdelecroix gravatar imagevdelecroix ( 2013-10-25 16:10:57 +0200 )edit

What @vdelecroix means is that this is a Sage forum and so it's hard to answer this in the forum. I'll try to do so briefly below.

kcrisman gravatar imagekcrisman ( 2013-10-25 17:03:01 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-10-25 17:06:15 +0200

kcrisman gravatar image

Numpy is not designed for arbitrary precision calculation, either in (decimal) real numbers or in integer arithmetic. It is hence significantly faster for data computation where such precision is not needed (which is most of data analysis in government, medicine, economics, etc.). In mathematics, where this is needed, we have things like Sage integers; these are NOT just "int"s, but will use up all your available memory if you let it.

The negative answer is a fun thing that happens with such integers - you literally went around the corner from infinity and through negative numbers! This is a very common behavior with integers - see the Numpy docs:

int64   Integer (-9223372036854775808 to 9223372036854775807)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-10-25 15:11:00 +0200

Seen: 516 times

Last updated: Oct 25 '13