Results 1 to 8 of 8

Thread: "math.h" VS "qmath.h"

  1. #1
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default "math.h" VS "qmath.h"

    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?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: "math.h" VS "qmath.h"

    Quote Originally Posted by digog View Post
    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.

  3. #3
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "math.h" VS "qmath.h"

    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().

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "math.h" VS "qmath.h"

    If you REALLY wanted to use a standard function, you could always call pow(x,2), but sqr() does not exist.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: "math.h" VS "qmath.h"

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

  6. #6
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "math.h" VS "qmath.h"

    Ok, thanks for the help.


    Added after 24 minutes:


    Quote Originally Posted by SixDegrees View Post
    A lot of people use a macro to perform this operation if they want to explicitly say SQR(x).
    How can I do that?
    Last edited by digog; 23rd April 2011 at 00:58.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: "math.h" VS "qmath.h"

    Qt Code:
    1. #define SQR(x) ((x)*(x))
    To copy to clipboard, switch view to plain text mode 
    The parentheses are deliberate and necessary to avoid surprises. Compare these results:
    Qt Code:
    1. #define SQR(x) ((x)*(x))
    2. y = 270.0 / SQR(3 + 3); // y = 7.5 as expected
    3.  
    4. #define SQR(x) (x)*(x)
    5. y = 270.0 / SQR(3 + 3); // y = 270.0, slightly confusing
    6.  
    7. #define SQR(x) x*x
    8. y = 270.0 / SQR(3 + 3); // y = 102, huh!
    To copy to clipboard, switch view to plain text mode 


    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.
    Last edited by ChrisW67; 23rd April 2011 at 03:49.

  8. The following user says thank you to ChrisW67 for this useful post:

    digog (24th April 2011)

  9. #8
    Join Date
    Aug 2010
    Posts
    32
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "math.h" VS "qmath.h"

    Thanks, that resolved my problem.

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 22:46
  2. Replies: 3
    Last Post: 15th February 2010, 18:27
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.