C# Float expression: strange behavior when casting the result float to int

First of all, I assume that you know that 6.2f * 10 is not exactly 62 due to floating point rounding (it’s actually the value 61.99999809265137 when expressed as a double) and that your question is only about why two seemingly identical computations result in the wrong value. The answer is that in the case … Read more

Is it possible to get 0 by subtracting two unequal floating point numbers?

In Java, a – b is never equal to 0 if a != b. This is because Java mandates IEEE 754 floating point operations which support denormalized numbers. From the spec: In particular, the Java programming language requires support of IEEE 754 denormalized floating-point numbers and gradual underflow, which make it easier to prove desirable … Read more

John Carmack’s Unusual Fast Inverse Square Root (Quake III)

FYI. Carmack didn’t write it. Terje Mathisen and Gary Tarolli both take partial (and very modest) credit for it, as well as crediting some other sources. How the mythical constant was derived is something of a mystery. To quote Gary Tarolli: Which actually is doing a floating point computation in integer – it took a … Read more

What is the difference between quiet NaN and signaling NaN?

When an operation results in a quiet NaN, there is no indication that anything is unusual until the program checks the result and sees a NaN. That is, computation continues without any signal from the floating point unit (FPU) or library if floating-point is implemented in software. A signalling NaN will produce a signal, usually … Read more

Why can’t I use float value as a template parameter?

THE SIMPLE ANSWER The standard doesn’t allow floating points as non-type template-arguments, which can be read about in the following section of the C++11 standard; 14.3.2/1      Template non-type arguments      [temp.arg.nontype] A template-argument for a non-type, non-template template-parameter shall be one of: for a non-type template-parameter of integral or enumeration type, a converted constant … Read more

How to extract a floating number from a string [duplicate]

If your float is always expressed in decimal notation something like >>> import re >>> re.findall(“\d+\.\d+”, “Current Level: 13.4db.”) [‘13.4’] may suffice. A more robust version would be: >>> re.findall(r”[-+]?(?:\d*\.\d+|\d+)”, “Current Level: -13.2db or 14.2 or 3”) [‘-13.2’, ‘14.2’, ‘3’] If you want to validate user input, you could alternatively also check for a float … Read more