This is completely a hypothetical question.
In below code, I am performing calculation, which is having one variable,z , as a value assigned "nan". Will main calculation will be slower as compared to normal z value (like z = 1.0)
float z = 0.0/0.0; // that means z is "nan"
float p = 50.0, q = 100.0, r = 150.0;
// main calculation Type 1
float c = ((x*100)+(y*100))/(x*100)+(y*100)+152*(p+q/r)+z;
Here is an example to show the main calculation with normal z value
float z = 1.0; // normal value for z
float p = 50.0, q = 100.0, r = 150.0;
// main calculation Type 2
float c = ((x*100)+(y*100))/(x*100)+(y*100)+152*(p+q/r)+z;
Hence, which one is slower, Type 1 or Type 2? Or there will be no time difference? In single calculation, time difference might not be visible, but, if we collect millions of such equations, how time results will vary?
Any kind of thought, logic or information will be appreciated.
Note: Currently i am not concerned about the result value of variable 'c';
Easily the strangest thing about floating-point numbers is the floating-point value “NaN”. Short for “Not a Number”, even its name is a paradox. Only floating-point values can be NaN, meaning that from a type-system point of view, only numbers can be “not a number”.
NaN, an acronym for Not a Number is an exception that usually occurs in the cases when an expression results in a number that is undefined or can't be represented. It is used for floating-point operations. For example: The square root of negative numbers. Division by zero.
To check whether a floating point or double number is NaN (Not a Number) in C++, we can use the isnan() function. The isnan() function is present into the cmath library. This function is introduced in C++ version 11.
Why does NaN exist? NaN is a commonly used term in coding to represent the output of a mathematical expression that isn't a number. It's not 0, it's not infinity, its just not a number that exists. Interestingly, if you run typeof NaN it will equal number — NaN is a number type.
It depends on the CPU. Old x86 intel chips, without SSE, handled NAN very badly (see http://www.cygnus-software.com/papers/x86andinfinity.html for a 10-year-old analysis.) However, SSE2/3/4 doesn't suffer from this problem, and I don't believe AMD ever did.
You may well see this problem on modern chips if you tell your compiler to avoid SSE instructions for possible compatibility (but that will definitely slow down floating point regardless of NaNs, so don't do it unless you have to.) As far as I know, this compatibility mode is still the gcc default for x86 builds.
I don't know about ARM floating point units; you'd have to test.
I suspect that NANs are not the issue that they were a decade ago, but enough people have a vague memory of those times that the prejudice undoubtedly will live on for a while longer.
Thinking critically is really making me challenge my understanding of C. I think the best answer is to experiment. Use time.h to save the clock time before and after each calculation and compute the difference.
#include<stdio.h>
#include<time.h>
clock_t t1, t2;
float result;
t1 = clock();
//perform calculation1
t2 = clock();
printf("%f", result);
t1 = clock();
//perform calculation2
t2 = clock();
printf("%f", result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With