Results 1 to 6 of 6

Thread: float

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default float

    Do anyone have an idea of this behaviour???
    Qt Code:
    1. cout << Density << "\n"; //this print 0.13
    2. float f = Density*100;
    3. slider1->setValue(f); //this set slider1 to 12!!!! why????
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. float f = 0.13*100;
    2. slider1->setValue(f); //this set slider1 to 13
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: float

    Probably due to limited precision of float and implicit floor casting to int.

    Did you ever try to compare float values ?
    Check this out:

    Qt Code:
    1. #include <iostream>
    2.  
    3. int main(){
    4. float n = 0.99;
    5. while(n!=0.09){
    6. std::cout << n << std::endl;
    7. n-=0.01;
    8. }
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Try this to make your app work:

    Qt Code:
    1. float f = Density*100;
    2. slider1->setValue((int)(0.5+f));
    To copy to clipboard, switch view to plain text mode 

    The second line is equivalent to
    Qt Code:
    1. slider1->setValue(round(f));
    To copy to clipboard, switch view to plain text mode 
    but Windows doesn't know how to round(), so a hack like (int)(0.5+f) is needed.

  3. The following user says thank you to wysota for this useful post:

    mickey (25th July 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    9
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: float

    Because in the fallow code line:

    Qt Code:
    1. float f = Density*100;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. float f = 0.13*100;
    To copy to clipboard, switch view to plain text mode 

    the number "100" in implicity cast to a float and this conversion could lead to a precision lost.

    Try to use:

    Qt Code:
    1. float f = Density*100.0;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. float f = 0.13*100.0;
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: float

    Quote Originally Posted by Zatraz
    Because in the fallow code line:

    Qt Code:
    1. float f = Density*100;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. float f = 0.13*100;
    To copy to clipboard, switch view to plain text mode 

    the number "100" in implicity cast to a float and this conversion could lead to a precision lost.

    Try to use:

    Qt Code:
    1. float f = Density*100.0;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. float f = 0.13*100.0;
    To copy to clipboard, switch view to plain text mode 
    sorry but your example doesn't solve the problem......
    Regards

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: float

    Try mine, it should work just fine.

  7. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: float

    Quote Originally Posted by wysota
    Try mine, it should work just fine.
    yes I just use it! I thanked you for this......
    Regards

Similar Threads

  1. Selecting a QLine
    By Paul Drummond in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2015, 16:30
  2. Slot
    By mickey in forum Qt Programming
    Replies: 13
    Last Post: 4th June 2006, 12:18

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.