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

Does one double promote every int in the equation to double?

I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent. This is not compiler dependent. C++ clearly defines the order of these operations and how they are converted. How the conversion happens is dependent on the order of operations. double result1 = … Read more

What is the size of float and double in C and C++? [duplicate]

Excerpt from the C99 standard, normative annex F (The C++-standard does not explicitly mention this annex, though it includes all affected functions without change per reference. Also, the types have to match for compatibility.): IEC 60559 floating-point arithmetic F.1 Introduction 1 This annex specifies C language support for the IEC 60559 floating-point standard. The IEC … Read more

Printing double without losing precision

It’s not correct to say “floating point is inaccurate”, although I admit that’s a useful simplification. If we used base 8 or 16 in real life then people around here would be saying “base 10 decimal fraction packages are inaccurate, why did anyone ever cook those up?”. The problem is that integral values translate exactly … Read more

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

Clarification on the Decimal type in Python

The Decimal class is best for financial type addition, subtraction multiplication, division type problems: >>> (1.1+2.2-3.3)*10000000000000000000 4440.892098500626 # relevant for government invoices… >>> import decimal >>> D=decimal.Decimal >>> (D(‘1.1’)+D(‘2.2’)-D(‘3.3’))*10000000000000000000 Decimal(‘0.0’) The Fraction module works well with the rational number problem domain you describe: >>> from fractions import Fraction >>> f = Fraction(1) / Fraction(3) >>> … Read more

Why is there no 2-byte float and does an implementation already exist?

TL;DR: 16-bit floats do exist and there are various software as well as hardware implementations There are currently 2 common standard 16-bit float formats: IEEE-754 binary16 and Google’s bfloat16. Since they’re standardized, obviously anyone who knows the spec can write an implementation. Some examples: https://github.com/ramenhut/half https://github.com/minhhn2910/cuda-half2 https://github.com/tianshilei1992/half_precision https://github.com/acgessler/half_float Or if you don’t want to use … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)