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

Floating point precision - does/ will this library support this? #13

Closed
4 tasks done
bguiz opened this issue Nov 30, 2016 · 3 comments
Closed
4 tasks done

Floating point precision - does/ will this library support this? #13

bguiz opened this issue Nov 30, 2016 · 3 comments

Comments

@bguiz
Copy link
Contributor

bguiz commented Nov 30, 2016

@kgryte Thanks for the quick merge earlier!

I have a question - is it the intent of this library's math section to provide a means to solve the floating point precision problems in JS?

A concise demonstration of what I'm referring to:

$ node 
> 0.3
0.3
> 0.1 + 0.2
0.30000000000000004

I came across this lib linked from this article and while reading it, this came to mind immediately.

For many common application tasks, minor deviations in precision are unlikely to have a significant effect. However, for numeric computing and applications requiring high precision, poor precision does have a significant effect due to accumulated error. Especially since the built-in Math functions are frequently used by higher-order functions, small deviations compounded many times can lead to significant drift from the "true" value.

... and that paragraph implied that you have solved (or are aiming to solve) this very problem, which I believe is an baseline factor that tends to affect precision in any non-integer math in JS. I've tried looking for references to this specific issue in this lib (and its FAQ), but have not been able to find reference to it yet.

Checklist

Please ensure the following tasks are completed before filing an issue.

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • If this is a general question, searched the FAQ for an existing answer.
  • If this is a feature request, the issue name begins with RFC: .

Description

Description of the issue (or feature request).

Does this lib provide a means to solve this?

$ node 
> 0.3
0.3
> 0.1 + 0.2
0.30000000000000004

Related Issues

Does this issue (or feature request) have any related issues?

None.

Questions

Any questions for reviewers?

Yes, see above.

Other

Any other information relevant to this issue (or feature request)? This may include screenshots, references, stack traces, sample output, and/or implementation notes.

Demo

If relevant, provide a link to a live demo.

Not applicable.

Reproduction

If this issue is a bug report, what steps are required to reproduce the unexpected output? (If this is a feature request, remove this section.)

Not applicable.

Expected Results

What are the expected results? (If this is a feature request, remove this section.)

The following results are expected:

(insert expected results here)

Actual Results

What are the actual results? (If this is a feature request, remove this section.)

The following are the actual results:

$ node 
> 0.3
0.3
> 0.1 + 0.2
0.30000000000000004
> stdlib.math.add([0.1, 0.2])
0.3

Environments

If this issue is a bug report, what environments are affected; e.g., Node v0.4.x, Chrome, IE 11? (If this is a feature request, remove this section.)

v8, likely all JS engine implementations

@kgryte
Copy link
Member

kgryte commented Nov 30, 2016

@bguiz Yeah, thanks for the question. This behavior is expected.

> 0.1 + 0.2
0.30000000000000004

This is not because JavaScript implementors have failed to properly implement floating-point math. The reason the answer is not exactly 0.3 is because not all numbers are exactly representable as double-precision floating-point numbers (see here). In short, addition is working as expected.

In general, this type of imprecision is not an issue, as floating-point rounding errors tend to cancel out. What we are concerned about is the types of algorithms used to implement basic math functions, such as trig and other special functions. The errors arising from these algorithms are of a different kind than the errors arising from floating-point math.

To "address" floating-point errors, you can use symbolic or arbitrary precision arithmetic libraries, which is not currently within the scope of this library, but may at some point in the future.

@bguiz
Copy link
Contributor Author

bguiz commented Nov 30, 2016

Thanks for the answer!

To "address" floating-point errors, you can use symbolic or arbitrary precision arithmetic libraries, which is not currently within the scope of this library, but may at some point in the future.

With the name stdlib I think including this at some point in the future might make sense. For example, you already have some util functions which overlap with these libraries, for example this one which converts a higher precision float to a lower precision one: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/utils/float64-to-float32

@kgryte
Copy link
Member

kgryte commented Nov 30, 2016

@bguiz Agreed. These libraries would be nice to have, but we first want to cover all our bases and get the basics right before moving on to higher order concerns. Much to do, so little time!

Also, if you have any other general questions, feel free to also seek us out and leave a message on Gitter.

@kgryte kgryte closed this as completed Dec 1, 2016
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

2 participants