Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C programming, will a calculation be slower, if any variable in the expression is nan

Tags:

c

nan

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';

like image 695
Vishwadeep Singh Avatar asked Sep 20 '13 06:09

Vishwadeep Singh


People also ask

What does NaN mean in calculation?

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”.

What does it mean NaN in C?

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.

How do I know if my floating-point is NaN?

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?

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.


2 Answers

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.

like image 162
rici Avatar answered Sep 30 '22 14:09

rici


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);
like image 34
I ate some tau Avatar answered Sep 30 '22 13:09

I ate some tau