5

Somebody asked this question on a programming contest:-

-1%1000000009 is -1 or 1000000008

I want to know, is this even possible? I tried in my system, got -1 every time. Also, I had to find out 10^-9 % 10^9, I used fmod and got answer 1e-009, shouldn't it be 1?

My interpretation:- 10^-9/10^9 = 1/10^18 So, answer = 1.

Please tell me where I am wrong.

unixia
  • 4,102
  • 1
  • 19
  • 23
  • As to your `fmod` question: [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de). – Jongware Jun 08 '14 at 12:43
  • 1
    @Jongware I am writing a program, so I need a clear view of what happens inside the program. This isn't helping. Any other help? – unixia Jun 08 '14 at 12:56
  • 1
    `%` is not a floating-point operator – phuclv Jun 08 '14 at 13:10
  • 2
    Improve your title please so that the question stands out – Lightness Races in Orbit Jun 08 '14 at 13:20
  • 2
    you need to differentiate modulo and remainder operation. In C, Java and most other languages it's remainder and produce -1 while python and some environments have true modulus operations and return 1000000008 – phuclv Jun 08 '14 at 13:21
  • http://stackoverflow.com/questions/17461486/what-are-the-rules-for-modular-arithmetic-in-c?lq=1 http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx http://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder – phuclv Jun 08 '14 at 13:22
  • My link is regarding the inaccuracies of floating point operations. That is why you get a small value where one would expect zero (*not* "1"). – Jongware Jun 08 '14 at 13:23
  • http://stackoverflow.com/questions/24074869/why-is-the-behavior-of-the-modulo-operator-different-between-c-and-ruby-for?lq=1 – phuclv Jun 08 '14 at 13:23
  • 1
    @Jongware: Huh? No. You wouldn't expect zero from that. That would make no sense. – tmyklebu Jun 08 '14 at 15:44

1 Answers1

3

preview : ( i will refer mod as %)

Just like in 1%3 , we do (int) 1/3 which is 0 , and then we ask : how many to add in order to get 1 ?

the answer is 1.

so 1%3=1.


Looking at 10^-9 % 10^9

let's use another numbers , for clarity :

2^-3 % 2^3

first we calc the integer value of the deviation:

2^-3 / 2^3 = 1/(2^3 * 2^3) = 1/64

as you can see it's a small number

so the int part is 0.

so - how many to add in order to get 2^-3 ? that's right : 2^-3


regarding your exact question :

My interpretation:- 10^-9/10^9 = 1/10^18 So, answer = 1.

1/10^18 indeed.

what's the integer part ? a zero.

from that zero , how much we need to add to get to -1 ?

yup , -1.

just follow the rules of Modulo .

first find the integer deviation. and then ask : how much we need to add in order to get to numerator .

edit:

for a situation where numerator >denominator

7 % 5 = > 7 /5 => 1.4 => .4 go to hell = > you're left with 1.

but notice.

this is 1 times 5.

ok so from 1 times 5 - how much it takes to go to 7 ? yes : 2.

more advanced :

3.111 %2 = > 3.111/2 = > 1.5555 => .555 go to hell => you're left with 1.

but that's 1 times of 2.

so from 1 times of 2 - how much it takes to go to 3.111 ? yup 1.111

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • Does it mean that in every case such that numerator is a floating point and denominator is integer type and quite bigger than the float, the answer would be the float(initially numerator) only? – unixia Jun 08 '14 at 13:05
  • @user3715736 even if the numerator has float with oranges and the denominator has apples with integers : you first(!) take the iteger part of the result(!) so `2.111 % 3 => 2.111/3 =>0.7033 => .7033 go to hell => you're left with 0 => so how much we need to get to 2.111 ? 2.111` – Royi Namir Jun 08 '14 at 13:10
  • 1
    `%` doesn't work with floating-point types. You can't write `3.111 % 2` – phuclv Jun 08 '14 at 13:24
  • 1
    @LưuVĩnhPhúc my whole answer was on the math perspective. and i refer to `%` as Modulo.( I use c# and it's valid statement). – Royi Namir Jun 08 '14 at 13:25
  • in that case use symbols like `mod` is better – phuclv Jun 08 '14 at 13:26
  • @Lưu Vĩnh Phúc Its just the notation, I have mentioned that I used fmod. Thanks btw! – unixia Jun 08 '14 at 13:30
  • float can't store such large precision and won't produce the correct result – phuclv Jun 08 '14 at 14:56