Ask Your Question
0

Problem with numpy integers and floats

asked 11 years ago

Mohamed Gaafar gravatar image

updated 11 years ago

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.

Preview: (hide)

Comments

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

vdelecroix gravatar imagevdelecroix ( 11 years ago )

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 ( 11 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 11 years ago

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)
Preview: (hide)
link

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: 11 years ago

Seen: 599 times

Last updated: Oct 25 '13