c++ - Why use fabs() rather than casting? -


i'm new c++, having learnt java previously. i'm struggling understand why use fabs() function (from <cmath>). understand fabs does, it's absolute value of number (i.e. more precision). however, can not in java:

int x = 1; float x = (float) x; 

what benefit in using fabs() function, rather casting?

it sounds don't entirely understand fabs - returns absolute value of number, i.e. y = |x|. if x positive returns x, if x negative if returns -x:

float x = 1.0f; float y = abs(x); // y = x = 1.0f  float x = -1.0f; float y = abs(x); // x = -1.0f, y = 1.0f 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -