How to produce a NaN float in c?

Using floating point numbers, 0.0 / 0.0 isn’t a “divide by zero” error; it results in NaN. This C program prints -nan: #include <stdio.h> int main() { float x = 0.0 / 0.0; printf(“%f\n”, x); return 0; } In terms what NaN looks like to the computer, two “invalid” numbers are reserved for “signaling” and … Read more

How to force GCC to assume that a floating-point expression is non-negative?

You can write assert(x*x >= 0.f) as a compile-time promise instead of a runtime check as follows in GNU C: #include <cmath> float test1 (float x) { float tmp = x*x; if (!(tmp >= 0.0f)) __builtin_unreachable(); return std::sqrt(tmp); } (related: What optimizations does __builtin_unreachable facilitate? You could also wrap if(!x)__builtin_unreachable() in a macro and call … Read more

What is float in Java?

In Java, when you type a decimal number as 3.6, its interpreted as a double. double is a 64-bit precision IEEE 754 floating point, while floatis a 32-bit precision IEEE 754 floating point. As a float is less precise than a double, the conversion cannot be performed implicitly. If you want to create a float, … Read more

Converting Int to Float or Float to Int using Bitwise operations (software floating point)

First, a paper you should consider reading, if you want to understand floating point foibles better: “What Every Computer Scientist Should Know About Floating Point Arithmetic,” http://www.validlab.com/goldberg/paper.pdf And now to some meat. The following code is bare bones, and attempts to produce an IEEE-754 single precision float from an unsigned int in the range 0 … Read more

“Expression is not assignable” — Problem assigning float as sum of two other floats in Xcode?

The other answers don’t exactly explain what’s going on here, so this is the basic problem: When you write blackKey.center.x, the blackKey.center and center.x both look like struct member accesses, but they’re actually completely different things. blackKey.center is a property access, which desugars to something like [blackKey center], which in turn desugars to something like … Read more