I'm not quite sure if this is the right forum to ask this question: I've been trying to implement the "floating-point modulus" function from the math library. Equivalently that's what I've tried in Python too. Problem is - the results are different and not even close. Please have a look at both snippets. Maybe someone can help me out:

[code = D]
import std.math;
import std.stdio;

immutable real MODULUS = 1e8;

real floatModPow(real base, ulong exp, real mod)
{
    real r = 1.0;

    while (exp) {
        if (exp & 1) r = fmod(r * base, mod);
        exp >>= 1;
        base = fmod(base*base, mod);
    }

    return r;
}

void main()
{
    floatModPow(3.866, 987654321UL, MODULUS).writeln;
}
[/code]

In this case the result is 6.44588e+07.

[code = Python]
import math

MOD = 1e8

def fpmod(b, e, m):
    r = 1.0
    while e:
        if (e & 1):
            r = math.fmod(r * b, m)
        e >>= 1
        b = math.fmod(b * b, m)
    return r

print (fpmod(3.866, 987654321, MOD))
[/code]

In this case the result is 82031250.0.

AFAIK the D.fmod and Python.fmod functions serve the same purpose. What am I missing?

Reply via email to