0

Following is the output of what I did in the python terminal.

math.modf(45.332)

(0.33200000000000074, 45.0)

So I felt that it was weird .... tried to mimic this modf function and got the same result..

k=lambda x : x-float(math.floor(x))

k(5.5)

0.5

k(45.332)

0.33200000000000074

Note : This is happening only when the floating point has more than 1 digit

martineau
  • 119,623
  • 25
  • 170
  • 301
user32457
  • 21
  • 1

1 Answers1

1

When you call modf(45.332) in your code the input will actually be converted to a floating point number with value 45.332000732421874.

In other words your problem happens before you are using the mimic function k(45.332), because that input argument will also be converted to float.

bogglez
  • 220
  • 2
  • 6
  • How does this help OP? Why would `45.332` in the script get converted to `45.332000732421874` in the first place (and note that that's the wrong value)? Isn't that confusing? There's a better answer to this, and it's already well covered in the proposed duplicate. – ChrisGPT was on strike May 24 '20 at 12:53
  • @Chris In addition to what is mentioned in the duplicate, OP seems to think that the problem lies with modf(), since he tried to implement a different function to replace it. So I'm just pointing out that the problem occurs before, and that it is due to "converted to a floating point number". – bogglez May 24 '20 at 12:56
  • Fair point. But glossing over why OP is dealing with `45.33200000000000074` in the first place is very confusing. This answer doesn't stand by itself. – ChrisGPT was on strike May 24 '20 at 12:57
  • Thank you ! wow computers are so nuanced .... – user32457 May 24 '20 at 13:10