-1

I have to solve a problem in C++, in which I have to calculate a modulus of a number formed as 10^n. But the problem is that n is a floating point number so if I calculate 10^n as pow(10.0, n) then it might overflow. So I'm seeking for a solution which can calculate 10^n mod m without any overflow problem. I'm normally use Python, where it's very straightforward, I don't know how to solve in C++.

horchler
  • 18,384
  • 4
  • 37
  • 73
nik_kgp
  • 1,112
  • 1
  • 9
  • 17
  • Modulus is not defined for floating-point numbers : if the exponent is floating-point, then the expression `X^e` could result in floating-point. – Nawaz Aug 10 '13 at 11:21
  • log(n) != 10^n... Do you mean exp(n)? – Oliver Charlesworth Aug 10 '13 at 11:21
  • my mistake, I have edited the question. If I am given with n = log(num) then I want to find 10^n % m, where n may be a floating point number. – nik_kgp Aug 10 '13 at 11:29
  • possible duplicate of [Can't use modulus on doubles?](http://stackoverflow.com/questions/9138790/cant-use-modulus-on-doubles) – Sergey Kalinichenko Aug 10 '13 at 11:30
  • I checked for floating numbers we can use fmod. So now, if we use fmod instead normal % operator then how can we calculate for large values of n? – nik_kgp Aug 10 '13 at 11:42
  • 2
    10^7.5 is about 31622776.601683793319988935444327 what answer do you want when taking this mod 6 ? – brian beuning Aug 10 '13 at 11:42
  • I expect 4.6. In Python I checked pow(10, 7.5) % 6 -> 4.6 while in C++ if I use fmod then I get 4. But I expect 4.6 and I want to achieve in C++. – nik_kgp Aug 10 '13 at 11:49
  • You will need a big floating point form to handle this. –  Aug 10 '13 at 14:15

1 Answers1

-2

How about rounding first, storing the digits smaller than 1 in a float variable (you will get them by substracting the rounded variable from the original float) and then do the modulo with the rounded value. Afterwards add up the float variable

Will this solve your problem?

EDIT (see comment and for better look ;-) )

 10^7.4 = 25118864.315[...]
 --> round = 25118864,
     float = 0.315,
     round % 6 = 2,

 --> (10^7.4)%6 = 2 + 0.315 = 2.315

as calc at windows would give you

Martin
  • 493
  • 6
  • 16
  • Consider this e.g. 10^7.4 mod 6. So the float value is 0.6 and rounded value is 7. 10^7 mod 6 gives 4 and if we add float value it is 4.6. And the right answer is 2.315. – nik_kgp Aug 10 '13 at 13:02
  • for better understanding ... 10^7.4 = 25118864.315[...] --> round = 25118864, float = 0.315, round % 6 = 2, (10^7.4)%6 = 2 + 0.315 = 2.315 which is what calc at windows would give you – Martin Aug 10 '13 at 13:11
  • But as I mentioned in my question, n may be large so if I do it in C++, it might overflow. How will I handle that situation? – nik_kgp Aug 10 '13 at 13:13
  • normally to prevent overflow you use log() in numerics. You could maybe implement an if greater than 10^5 or so, and then use log() before round, ... but the difficulty is then to implement modulo correctly transformed to log – Martin Aug 10 '13 at 13:18
  • Yes, I started my question that way if you look at my edited history. Still looking for an answer. – nik_kgp Aug 10 '13 at 13:22