0

I was creating a check to see if a number is a multiple of 0.1. For example, 56, 56.1, and 56.5 would work, but not 56.11, since it is a multiple of 0.01, but not 0.1. I thought I could use modular arithmetic to check for this using this expression:

number mod 0.1

Normally, when a number is evenly divisible by the number it is being modulated by (if that is the right term), the expression returns 0 (e.g. 10 mod 2 is 0, 16.5 mod 0.25 is 0, etc). However, however, when I do mod 0.1, it returns 0.1 when it is evenly divisible. For example, when "number" has a value of 56, 56.1, and 56.5, the expression returns 0.1, while 56.11 mod 0.1 has a value of 0.01.

I have tried this with other decimal numbers, and for some reason. modulating by 0.1 is the only case where it is evenly divisible if the expression returns 0.1 instead of 0.

I do not need to know this for a project or school, but it would be nice to know, and I can't seem to find the answer. Thanks!

Max
  • 1
  • rounding issues. $\frac{1}{10}$ cannot be expressed precisely in your computer's floating point binary – Henry Feb 03 '21 at 00:49
  • Sounds more like a programming question. What is happening to your input is because your computer saves the number "0.1" as a number close to but not exactly 0.1. Have a look here. – Kyan Cheung Feb 03 '21 at 00:54
  • It seems you seek to test if $10x$ is an integer, for $x$ a real (or float). Is that correct? – Bill Dubuque Feb 03 '21 at 02:06
  • I'm not sure if it's a programming or float number issue, since I see this issue across multiple calculators, including Google. – Max Feb 03 '21 at 17:21

0 Answers0