precision
C# double to decimal precision loss
138630.78380386264 is not exactly representable to double precision. The closest double precision number (as found here) is 138630.783803862635977566242218017578125, which agrees with your findings. You ask why the conversion to decimal does not contain more precision. The documentation for Convert.ToDecimal() has the answer: The Decimal value returned by this method contains a maximum of 15 significant … Read more
Find angle between hour and minute hands in an analog clock
It turns out that Wikipedia does have the best answer: // h = 1..12, m = 0..59 static double angle(int h, int m) { double hAngle = 0.5D * (h * 60 + m); double mAngle = 6 * m; double angle = Math.abs(hAngle – mAngle); angle = Math.min(angle, 360 – angle); return angle; } … Read more
Set back default floating point print precision in C++
You can get the precision before you change it, with std::ios_base::precision and then use that to change it back later. You can see this in action with: #include <ios> #include <iostream> #include <iomanip> int main (void) { double pi = 3.141592653590; std::streamsize ss = std::cout.precision(); std::cout << “Initial precision = ” << ss << ‘\n’; … Read more
How to perform unittest for floating point outputs? – python
The precision of float in Python is dependent on the underlying C representation. From Tutorial/Floating Point Arithmetic: Issues and Limitations, 15.1: Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. As for testing, a better idea is to use existing functionality, e.g. … Read more
How to convert milliseconds to seconds with precision
Surely you just need: double seconds = milliseconds / 1000.0; There’s no need to manually do the two parts separately – you just need floating point arithmetic, which the use of 1000.0 (as a double literal) forces. (I’m assuming your milliseconds value is an integer of some form.) Note that as usual with double, you … Read more
Show two digits after decimal point in c++ [duplicate]
cout << fixed << setprecision(2) << total; setprecision specifies the minimum precision. So cout << setprecision (2) << 1.2; will print 1.2 fixed says that there will be a fixed number of decimal digits after the decimal point cout << setprecision (2) << fixed << 1.2; will print 1.20
How to manually parse a floating point number from a string
All of the other answers have missed how hard it is to do this properly. You can do a first cut approach at this which is accurate to a certain extent, but until you take into account IEEE rounding modes (et al), you will never have the right answer. I’ve written naive implementations before with … Read more