Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result when multiplying a very small number and a very big one using ^ #27355

Closed
ronisbr opened this issue May 31, 2018 · 7 comments
Closed

Comments

@ronisbr
Copy link
Member

ronisbr commented May 31, 2018

Hi guys,

I was implementing a algorithm, comparing to MATLAB version, and I found a very strange behavior in Julia. I need to perform the following multiplication:

4.604716014154575e-19*(10000000000)^2 = -46.047160141545149

First, I was using the ^, but it was providing a wrong answer:

julia> 4.604716014154575e-19*10000000000^2
3.5761512189350633

If I drop the ^ and multiply two times, then the result is correct:

julia> 4.604716014154575e-19*10000000000*10000000000
46.04716014154576

I tested it in Julia v0.6.2 and v0.6.3 in macOS and Linux with the same behavior. I think the operation 10000000000^2 is overflowing Int64 and with ^ this is computed first. On the other hand, using the multiplication, the first operation is 4.604716014154575e-19*10000000000, which does not cause any overflow.

However, I think Julia should be able to handle that just as MATLAB can. At least a warning about the overflow should be printed. Otherwise, sometimes can be very difficult to debug this. What do you think?

@mbauman
Copy link
Member

mbauman commented May 31, 2018

Duplicate of #855

@mbauman mbauman marked this as a duplicate of #855 May 31, 2018
@mbauman mbauman closed this as completed May 31, 2018
@mbauman
Copy link
Member

mbauman commented May 31, 2018

Note that this works in Matlab because the literal 10000000000 is treated as a floating point number. To duplicate that behavior you can do 4.604716014154575e-19*10000000000.0^2.

@ronisbr
Copy link
Member Author

ronisbr commented May 31, 2018

@mbauman thanks! I will follow that bug.

@ViralBShah
Copy link
Member

Its best to ask these questions on discourse.julialang.org.

@ronisbr
Copy link
Member Author

ronisbr commented Jun 1, 2018

Sorry but it really looked like a bug to me. Actually, IMHO, I think this should be considered a bug.

@StefanKarpinski
Copy link
Member

You may not like it but it is not a bug as it is both intentional and thoroughly documented.

@ronisbr
Copy link
Member Author

ronisbr commented Jun 1, 2018

Ok then...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants