PDA

View Full Version : "math.h" VS "qmath.h"



digog
22nd April 2011, 02:39
What's the difference between the math.h and qmath.h??
I mean, if I need to call a square root function whats the difference between sqrt() and qSqrt()?
And where can I find a square function?

ChrisW67
22nd April 2011, 03:37
What's the difference between the math.h and qmath.h??
One is a standard C header, the other is part of Qt.

I mean, if I need to call a square root function whats the difference between sqrt() and qSqrt()?
sqrt() is the standard C function defined for double.
qSqrt() is a Qt function defined in terms of qreal, either a float or double depending on platform, which calls a type specific sqrt() implementation. Read the header file for more details.
There should be no difference in result on most platforms where qreal == double.

And where can I find a square function?
You are joking, aren't you?

BTW: Asking the same thing in two places is bad form.

digog
22nd April 2011, 22:19
You are joking, aren't you?

No, I'm not. I know that I can do x*x, and that is very simple to implement a function to do that. But I though that it was a standart funtion like in pascal, where you can call sqr().

squidge
22nd April 2011, 23:18
If you REALLY wanted to use a standard function, you could always call pow(x,2), but sqr() does not exist.

SixDegrees
23rd April 2011, 00:11
Note that pow() normally uses a polynomial series to do its computation, which will result in a LOT more math operations than simply multiplying two numbers once.

A lot of people use a macro to perform this operation if they want to explicitly say SQR(x).

digog
23rd April 2011, 00:58
Ok, thanks for the help.

Added after 24 minutes:


A lot of people use a macro to perform this operation if they want to explicitly say SQR(x).

How can I do that?

ChrisW67
23rd April 2011, 03:34
#define SQR(x) ((x)*(x))

The parentheses are deliberate and necessary to avoid surprises. Compare these results:


#define SQR(x) ((x)*(x))
y = 270.0 / SQR(3 + 3); // y = 7.5 as expected

#define SQR(x) (x)*(x)
y = 270.0 / SQR(3 + 3); // y = 270.0, slightly confusing

#define SQR(x) x*x
y = 270.0 / SQR(3 + 3); // y = 102, huh!



You could define an inline free function to do it. This gives you type checking that the macro will not and can (possibly) be inlined to avoid function call overhead.

digog
23rd April 2011, 21:31
Thanks, that resolved my problem.