Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module computation on float #2504

Open
denis-migdal opened this issue Oct 23, 2024 · 0 comments
Open

Module computation on float #2504

denis-migdal opened this issue Oct 23, 2024 · 0 comments

Comments

@denis-migdal
Copy link
Contributor

Hi,

Currently, float modulo is computed as:

if($B.$isinstance(other, _b_.int)){
        other = _b_.int.numerator(other)
        return fast_float((self.value % other + other) % other)
    }

    if($B.$isinstance(other, float)){
        // use truncated division
        // cf https://en.wikipedia.org/wiki/Modulo_operation
        var q = Math.floor(self.value / other.value),
            r = self.value - other.value * q
        if(r == 0 && other.value < 0){
            return fast_float(-0)
        }
        return fast_float(r)
    }

Is there a reason to not simply use the following formula ?

const mod = (a % b + b) % b; // the formula used everywhere in the code.
if( mod === 0 && b < 0 )         // handle -0.
    return -0;
return mod;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant