0

I am getting unexpected results when multiplying 2 fractional numbers.

If i use a calculator (google) to check my sums, i get the correct answer (or the answer i am expecting)

This is an example of the sum i am trying to do abs(-0.00012437234926353282 * 0.2) (Note: the first number is not always negative, but i want the abs result of the sum).

A calculator gives me this answer: 0.00002487446

But python gives me this 2.4874469852706566e-05

The simple code i am using is this

x = y = round(abs(-0.00012437234926353282 * 0.2), 30)

I am kind of new to Python, please be kind.

I have tried reading up on it, and lots of methods, including floating, rounding and a few more in combinations and different orders, etc (all clearly wrong methods), but all get the "unexpected" results

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jon
  • 3
  • 4
  • That is literally the same result. The e-0.5 signifies an exponent of -0.5, i.e 10 to the power of -0.5, which translates to 0.00001 being multiplied to the original number, which gives you 0.000024874469852706566. – A-T Feb 25 '23 at 17:10
  • @A-T It's `-05`, not `-0.5`. `-0.5` would be the reciprocal of the *square root* instead of the reciprocal of `1e5`. – wjandrea Feb 25 '23 at 17:14
  • Thanks for the correction @wjandrea. As you can see, I meant to write e-05 in my explanation but ended up writing -0.5 for some reason unclear to me now too. God knows what I was thinking while writing it. – A-T Feb 25 '23 at 17:45

1 Answers1

1

If I am not mistaken, python does give the correct result: 2.4874469852706566e-05 means 2.4874469852706566 * 10**(-5) which is the same as google's result of 0.00002487446, just rounded to a different decimal place.

Nonlinear
  • 684
  • 1
  • 12
  • You are correct; the 'e' thing is _scientific notation_ – Emanuel P Feb 25 '23 at 17:09
  • It's not exactly the same though; the last digit is 6 in Google's result, but the Python result would be rounded to 7 at the same spot. – wjandrea Feb 25 '23 at 17:11
  • 1
    @wjandrea I've checked on [google](https://www.google.com/search?q=2%2F3&rlz=1C1ONGR_deDE982DE982&ei=lkL6Y9KIMZ2Bxc8P97yM0Ak&ved=0ahUKEwjSq8bUlrH9AhWdQPEDHXceA5oQ4dUDCA8&uact=5&oq=2%2F3&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIFCAAQkQIyBQgAEJECMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgkIABAHEB4Q8QQyBggAEAcQHjIFCAAQgAQyBggAEAcQHjIJCAAQBxAeEPEEOgoIABBHENYEELADSgQIQRgAULkEWMkGYK4HaAFwAXgAgAFPiAGZAZIBATKYAQCgAQHIAQjAAQE&sclient=gws-wiz-serp), and it seems that it doesn't round numbers, but rather truncates them. – Nonlinear Feb 25 '23 at 17:18
  • @user Those comparisons are both `True`. You can plug them right into Python and see that; you don't need to use `if`. Plus, if you plug in `0.00002487446`, Python represents it as `2.487446e-05`. – wjandrea Feb 25 '23 at 17:32
  • OK, thank you. my lack of math understanding then! So would this be viable code? if 2.4874469852706566e-05 < 0.001: print("less") else: print("more") it does register as less! (it just does not register in my mind as less :-) ) What i am doing it making LOTS of calculations, so am outputting the results to check i coded it correct, yet "2.4874469852706566e-05" looked incorrect or not what i was expecting to see! – Jon Feb 25 '23 at 17:36
  • Thank you all! It was my mistake (of course always is with code!) even though it didn't look correct in my test output, it was functioning correct! I now know for future that python is better at math than i am :-) – Jon Feb 25 '23 at 23:57