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

Unexpected NaN from sqrt() #164

Open
hypcos opened this issue Mar 25, 2024 · 1 comment
Open

Unexpected NaN from sqrt() #164

hypcos opened this issue Mar 25, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@hypcos
Copy link

hypcos commented Mar 25, 2024

I expected that X.sqrt() should give similar result to X.pow(0.5), but it doesn't.
Especially when the layer is 1.
Example:

> new Decimal(1e-16).pow(0.5)+''
< '1.000000000000002e-8'
> new Decimal(1e-16).sqrt()+''
< 'NaN'
> new Decimal(1e16).neg().pow(0.5)+''
< 'NaN'
> new Decimal(1e16).neg().sqrt()+''
< '99999999.99999979'
@James103
Copy link

James103 commented Jul 4, 2024

This is caused by incorrect handling of negative mag in sqrt:

} else if (this.layer === 1) {
return FC(1, 2, Math.log10(this.mag) - 0.3010299956639812);

When layer is 1 and mag is negative, the sqrt function tries to take the logarithm of a negative number, which is NaN.

@MathCookie17 MathCookie17 added the bug Something isn't working label Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants