18 votes
Accepted

IEEE 754 square root with Newton-Raphson

This answer uses pointer-casting for type-punning just to save space. In practice keep using your union (safe in ISO C99, and in C++ as a GNU and MSVC extension) or memcpy (safe in C and C++). This ...
Rainer P.'s user avatar
  • 2,622
15 votes
Accepted

Floating point approximately equal

When we multiply epsilon by the largest number (absolute value, as noted in chux's answer), we end up with a value that's about ...
Toby Speight's user avatar
  • 74.9k
13 votes

Checking for NaN bit patterns in C++

First of all, the compiler is not hostile; by using -ffast-math you are asking for an optimization that the compiler dutifully performs. Perhaps it is not what you ...
G. Sliepen's user avatar
  • 55.4k
12 votes
Accepted

A function to convert 0-2000 milliseconds to a double representing a fraction of a second

random garbage in initial word // fakeDouble is expected to be {0, 0} This is insanity. The code needs to implement this comment. Either ...
J_H's user avatar
  • 24.8k
11 votes
Accepted

`atof` implementation

FP overflow A ridiculously long fractional part causes decplace to reach inf, and the perfectly valid number is converted to <...
vnp's user avatar
  • 56.4k
11 votes

A function to convert 0-2000 milliseconds to a double representing a fraction of a second

Missing header uint32_t is not defined - we need to include <stdint.h> for this to compile. Unit tests The tests are ...
Toby Speight's user avatar
  • 74.9k
11 votes

A function to convert 0-2000 milliseconds to a double representing a fraction of a second

Code fails every time With ms == 0, code can return a non-zero fake double due to unassigned fakeDouble[]. Non-zero ...
chux - Reinstate Monica's user avatar
10 votes

`atof` implementation

Bug If I convert "0e+1", I will get back 1, because of these lines: ...
JS1's user avatar
  • 28.5k
10 votes
Accepted

String to double without std::stod

Don't use #define!!! There are much better techniques for all use cases of #define in C++. ...
Martin York's user avatar
10 votes

IEEE 754 square root with Newton-Raphson

FYI, in IEEE754 sqrt is a "basic" operation that's required to be correctly-rounded (rounding error <= 0.5ulp), same as + - * /. Hardware FPUs (I think) always ...
Peter Cordes's user avatar
  • 3,346
10 votes

Floating point approximately equal

Right goal? "difference is less than machine epsilon scaled by the largest number." l * e scales epsilon by the ...
chux - Reinstate Monica's user avatar
9 votes
Accepted

Calculating the number of coins in money change

In general we want to initialize a variable directly to the value it needs, rather than to a temporary "invalid" value. This reduces complexity, and helps to prevent the accidental use of the "invalid"...
user673679's user avatar
  • 11.7k
8 votes
Accepted

Rewritten implementation of quadratic equation formula

Aside from re-computing discriminant(), a reasonable quadratic implementation. When assessing the quality of floating-point computations, use ...
chux - Reinstate Monica's user avatar
8 votes

String to double without std::stod

I'm not sure you're allowed to define your own INT_MAX like this: #ifndef INT_MAX #define INT_MAX 2147483647 #endif // INT_MAX ...
Toby Speight's user avatar
  • 74.9k
8 votes
Accepted

std::isnormal in C#

Code is formally correct, just change the name to IsNormal() (PascalCase) to follow standard .NET naming conventions. However your second question "Is this ...
Adriano Repetti's user avatar
8 votes

IEEE 754 square root with Newton-Raphson

I don't see why you define this constant yourself: #define MANTISSA_SIZE 52 Given we already assume that FLT_RADIX is 2, we ...
Toby Speight's user avatar
  • 74.9k
8 votes
Accepted

An is_integer Template Function Implementation in C++

This is wrong for several reasons. What do you mean by "is an integer"? There are many ways to interpret the assertion that some number "is an integer". Do you mean it's an integer ...
G. Sliepen's user avatar
  • 55.4k
7 votes

Function to extract float from different price patterns

I agree that this is a little complicated. I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like, ...
ndp's user avatar
  • 2,361
7 votes

Checking for NaN bit patterns in C++

You have missed the namespace qualifier of std::uint64_t. It seems that std::uint_fast64_t should be sufficient, to make the ...
Toby Speight's user avatar
  • 74.9k
6 votes

Computing the square root of a 64-bit integer

Your code is OK for prime sieving. If the rounding mode is nearest-ties-to-even and std::sqrt is correctly rounded (as required by IEEE 754), your code returns \$\...
Řrřola's user avatar
  • 161
6 votes

BigDecimal class with arbitrary precision

I don't understand the "max precision" logic at all. If 20 is the max, then why is the precision a big integer? Just make it an integer if it can't get bigger than 20. But... ...if 20 is the max, ...
Eric Lippert's user avatar
  • 14.9k
6 votes

Swift FloatingPoint rounded to places

First of all one should be aware that most decimal fractions cannot be represented exactly by a binary floating point number. For example, ...
Martin R's user avatar
  • 23.2k
6 votes

Convert float price to integer price without commas

For me at least, it would be more natural to think of the operation as a multiplication by 100: ...
Janne Karila's user avatar
  • 10.3k
6 votes

String to double without std::stod

You are passing the string by value. prepNumber run(std::string init_string); there is no reason to copy the string. ...
JDługosz's user avatar
  • 11.3k
6 votes
Accepted

Function to print a double - exactly

An interesting curiosity, as you say. Because 10 is an exact multiple of 2, all binary fractions have an exact, non-repeating decimal representation (but not vice versa). That means we're guaranteed ...
Toby Speight's user avatar
  • 74.9k
6 votes

Conversion from string to long double

Formatting: Formatting is important for readability. Use spaces around operators. Add an empty line between sections of code (like paragraphs). Be consistent with indenting. Indent ...
user673679's user avatar
  • 11.7k
5 votes
Accepted

Floating point comparison program

A good effort with lots of useful debug to see the inner working of the task. Biggest design concern: lack of control on how "almost" almost_equals() operates. I'...
chux - Reinstate Monica's user avatar
5 votes

WGS84-coordinate encoded as Integers

you can avoid stuffing everything in a one-liner: ...
ratchet freak's user avatar
5 votes
Accepted

Computing the integer square root of large numbers

Your function is rather unnecessary. When converted to double, all 64-bit integers retain 1 sign bit and at least 52 precision bits. So the error is at most 11 bit; which means for any number N, the ...
W. Chang's user avatar
  • 379
5 votes

Rewritten implementation of quadratic equation formula

You are computing the discriminant too many times. For example, in the root1, root2 and the condition in the ...
user8786753's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible