## Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

Historical note: the comment thread below may refer to first and second implementations. I swapped the order in September 2017 since leading with a buggy implementation caused confusion. If you want something that maps “0.1e-100” to 101, then you can try something like function decimalPlaces(n) { // Make sure it is a number and use … Read more

## Precision in C floats

“6 digits after the decimal point” is nonesnse, and your example is a good demonstration of this. This is an exact specification of the float data type. The precision of the float is 24 bits. There are 23 bits denoting the fraction after the binary point, plus there’s also an “implicit leading bit”, according to … Read more

Categories c

## Determine precision and scale of particular number in Python

Getting the number of digits to the left of the decimal point is easy: int(log10(x))+1 The number of digits to the right of the decimal point is trickier, because of the inherent inaccuracy of floating point values. I’ll need a few more minutes to figure that one out. Edit: Based on that principle, here’s the … Read more

## C++ calculating more precise than double or long double

You will need to perform the calculation using some other method than floating point. There are libraries for doing “long math” such as GMP. If that’s not what you’re looking for, you can also write code to do this yourself. The simplest way is to just use a string, and store a digit per character. … Read more

Categories r

## Why is a round-trip conversion via a string not safe for a double?

I found the bug. .NET does the following in clr\src\vm\comnumber.cpp: DoubleToNumber(value, DOUBLE_PRECISION, &number); if (number.scale == (int) SCALE_NAN) { gc.refRetVal = gc.numfmt->sNaN; goto lExit; } if (number.scale == SCALE_INF) { gc.refRetVal = (number.sign? gc.numfmt->sNegativeInfinity: gc.numfmt->sPositiveInfinity); goto lExit; } NumberToDouble(&number, &dTest); if (dTest == value) { gc.refRetVal = NumberToString(&number, ‘G’, DOUBLE_PRECISION, gc.numfmt); goto lExit; } DoubleToNumber(value, … Read more

## Get DateTime.Now with milliseconds precision

How can I exactly construct a time stamp of actual time with milliseconds precision? I suspect you mean millisecond accuracy. DateTime has a lot of precision, but is fairly coarse in terms of accuracy. Generally speaking, you can’t. Usually the system clock (which is where DateTime.Now gets its data from) has a resolution of around … Read more

Categories c#

Categories r