number-precision | fast lib for doing addition , subtraction , multiplication | Math library
kandi X-RAY | number-precision Summary
kandi X-RAY | number-precision Summary
1K tiny & fast lib for doing addition, subtraction, multiplication and division operations precisely
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of number-precision
number-precision Key Features
number-precision Examples and Code Snippets
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function (error, results, fields) {
if (error) throw error;
console.log(results.insertId);
});
public static GeoHash withBitPrecision(double latitude, double longitude, int numberOfBits) {
if (numberOfBits > MAX_BIT_PRECISION) {
throw new IllegalArgumentException("A Geohash can only be " + MAX_BIT_PRECISION + " bits long!");
}
if (
Community Discussions
Trending Discussions on number-precision
QUESTION
Is it possible to keep trailing or leading zeroes on a number in javascript, without using e.g. a string instead?
...ANSWER
Answered 2022-Jan-07 at 08:40No. A number
stores no information about the representation it was entered as, or parsed from. It only relates to its mathematical value. Perhaps reconsider using a string
after all.
If i had to guess, it would be that much of the confusion comes from the thought, that numbers, and their textual representations would either be the same thing, or at least tightly coupled, with some kind of bidirectional binding between them. This is not the case.
The representations like 0.1
and 0.10
, which you enter in code, are only used to generate a number. They are convenient names, for what you intend to produce, not the resulting value. In this case, they are names for the same number. It has a lot of other aliases, like 0.100
, 1e-1
, or 10e-2
. In the actual value, there is no contained information, about what or where it came from. The conversion is a one-way street.
When displaying a number as text, by default (Number.prototype.toString
), javascript uses an algorithm to construct one of the possible representations from a number. This can only use what's available, the number value, also meaning it will produce the same results for two same numbers. This implies, that 0.1
and 0.10
will produce the same result.
Concerning the number1 value, javascript uses IEEE754-2019 float642. When source code is being evaluated3, and a number literal is encountered, the engine will convert the mathematical value the literal represents to a 64bit value, according to IEEE754-2019. This means any information about the original representation in code is lost4.
There is another problem, which is somewhat unrelated to the main topic. Javascript used to have an octal notation, with a prefix of "0". This means, that 003
is being parsed as an octal, and would throw in strict-mode. Similarly, 010 === 8
(or an error in strict-mode), see Why JavaScript treats a number as octal if it has a leading zero
In conclusion, when trying to keep information about some representation for a number (including leading or trailing zeroes, whether it was written as decimal, hexadecimal, and so on), a number
is not a good choice. For how to achieve some specific representation other than the default, which doesn't need access to the originally entered text (e.g. pad to some amount of digits), there are many other questions/articles, some of which were already linked.
[1]: Javascript also has BigInt
, but while it uses a different format, the reasoning is completely analogous.
[2]: This is a simplification. Engines are allowed to use other formats internally (and do, e.g. to save space/time), as long as they are guaranteed to behave like an IEEE754-2019 float64 in any regard, when observed from javascript.
[3]: E.g. V8 would convert to bytecode earlier than evaluation, already exchanging the literal. The only relevant thing is, that the information is lost, before we could do anything with it.
[4]: Javascript gives the ability to operate on code itself (e.g. Function.prototype.toString
), which i will not discuss here much. Parsing the code yourself, and storing the representation, is an option, but has nothing to do with how number
works (you would be operating on code, a string
). Also, i don't immediately see any sane reason to do so, over alternatives.
QUESTION
I'm building a multi-client javascript game which includes very large flocks/swarms of units of located on a Cartesian plane - location, vectors and acceleration rates are all very finely tuned and constantly affected by each other and other actors - and as such are stored with javascript floats.
For various reasons (speed of game functions and bandwidth limits) much of the logic is done in parallel on client computers running identical games based on common inputs/instructions.
I'm concerned that some permutations of [browser/OS/hardware] will store floats differently in memory which will result in marginal drift between game versions.
The traditional issues with floats are well documented and I'm not concerned with using them - they're clearly the best degree of precision for my purposes. (e.g.
...ANSWER
Answered 2020-Jul-02 at 09:55Yes, IEE 754 is well specified, and is referenced in the ECMA262 specification.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install number-precision
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page