Giter Site home page Giter Site logo

Instantiating from floats about math HOT 5 CLOSED

brick avatar brick commented on August 28, 2024
Instantiating from floats

from math.

Comments (5)

BenMorel avatar BenMorel commented on August 28, 2024 3

Do floats always approximate consistently?

You're getting me out of my comfort zone ;-)

I would suggest you try the same set of computations with both double and BigDecimal, measure the difference, and decide if the imprecision is within acceptable bounds!

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Hi 👋

I apologize if the README is not clear enough on this point. The suggestion really is to not use floats at all, as you'll most likely be bitten at some point by a rounding issue.

Floats can accurately represent integers (up to a point), halves, quarters, etc. Pretty much everything else is an approximation, that may or may not give the value you expect when converted to a string.

Example from this IEEE 754 converter:

You entered: 2.5
Value actually stored in float: 2.5

You entered: 2.4
Value actually stored in float: 2.400000095367431640625

As you can see though, (string) 2.4 in PHP still returns '2.4'. PHP is usually good at converting the value to the string you expect. But you can't be 100% confident that this will always be the case.

This is why the README advocates to deal with integer or string values everywhere outside BigNumber.

Back to your question.

In your case, you have a float value. Converting it to a string using (string) or strval() will not change anything, as this is what of() does internally. It will just fool of() into thinking that it was given a precise value.

What you should really do is refactor your codebase to only ever deal with integers or strings. That is, if you care about precision (when dealing with monetary values, for example).

EDIT: I should mention that the floats I'm working with are provided from another library. In other words, I am not creating these values myself, but instead performing calculations on generated ones.

If you really care about precision, you should not be using said library. Floating point values are inherently approximations in mose cases, and are not there for this purpose.

If you want to deal with large values but do not care about precision, then it may be OK to use the library in question. But then I would question why you'd need brick/math at all; if precision is not a requirement, you may perform calculations on floats directly.

If you give me the name of the library in question, I can probably better clarify this point.

Can we improve the README?

If there's some changes in wording I can make to the README to make this more clear, please let me know!

from math.

kevindecapite avatar kevindecapite commented on August 28, 2024

Thank you for your thorough and helpful reply :-)

The library I'm using is this one:

https://github.com/cyjoelchen/php-sweph

Which is a PHP extension of this C library:

https://www.astro.com/swisseph/swephinfo_e.htm

The underlying C lib uses double, which may be different from a float, although I'll gladly show my ignorance there.

As for overall precision, I would like my codebase to maintain the same level of precision as the Swiss Ephemeris library.

from math.

BenMorel avatar BenMorel commented on August 28, 2024

Thanks for the link!

PHP's floats are double under the hood, so they're the same thing (technically, usually float is 32-bit and double is 64-bit, aka double precision, but in PHP there's a single type, float, that's always 64-bit).

Your library deals with planetary positions, so inherently approximative. Floating-point values are a very good fit for this. Therefore I stand by my statement above, you may not need brick/math at all?

The only advantage of using brick/math to perform further calculations on your floating point values, is that you won't accumulate imprecision. At the cost of speed!

from math.

kevindecapite avatar kevindecapite commented on August 28, 2024

accumulate imprecision.

That's my concern because I do perform calculations on the returned data. Even though, as you stated, the source data is approximate anyway, I still want to avoid moving further away from those approximations by using the float primitive.

Do floats always approximate consistently? I would think they must, but this is part of my hesitation in using them and why I'm drawn to brick/math instead.

from math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.