Results 1 to 7 of 7

Thread: very simple precision question

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default very simple precision question

    I've an ofstream, "fout", that I want to set the precision for... but just to the right of the decimal. If the input is 315.12345678 and I set precision like so:

    Qt Code:
    1. fout << setprecision(5);
    To copy to clipboard, switch view to plain text mode 

    I get output like so: 315.12

    instead of 315.12346, which is what I want. How can I set precision such that it affects just to the right of the decimal, regardless of what's left of the decimal?

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

    Default Re: very simple precision question

    You are using floats and not doubles. You have to specifically use doubles, otherwise you won't get the precision you require.

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very simple precision question

    could you be more specific? (like how to specify float or double)

    The actual input data (loaded into the program using ifstream) might look like this:

    3123456
    3123456.23
    3123456.342356
    -- or --
    -134
    -134.34
    -134.3485938485

    These are the 2 main types of geographic coordinates I'm working with: UTM (first 3) or Latitue/Longitude. Basically what I want to do--regardless of what the input looks like--is be able to set the output so that there will be 2 values right of the decimal... whether the input is float, double, int, long, etc.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: very simple precision question

    Like this:
    http://www.cplusplus.com/reference/i...precision.html

    You have to write doubles in the stream, not floats.

    Regards

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

    Default Re: very simple precision question

    Quote Originally Posted by vonCZ View Post
    The actual input data (loaded into the program using ifstream) might look like this:
    If you load it using ifstream then you can load it directly into doubles and you'll be fine.

    Basically what I want to do--regardless of what the input looks like--is be able to set the output so that there will be 2 values right of the decimal... whether the input is float, double, int, long, etc.
    You'll have to provide some general mechanism for that. The easiest I can think of is to have several implementations of the same base class that do the reading, parsing and whatever else you may need. Something like (that's a major simplification):
    Qt Code:
    1. struct MyDataStruct {
    2. float long;
    3. float lat;
    4. };
    5.  
    6. class Base {
    7. virtual ~Base(){}
    8. virtual bool canHandle(const std::string &format) const = 0;
    9. virtual MyDataStruct getData() = 0;
    10. };
    11.  
    12. class Doubles : public Base {
    13. Doubles(std::ifstream &input) : Base(), str(input){
    14.  
    15. }
    16. bool canHandle(const std::string &format) const{ return (format=="DOUBLE"); }
    17. MyDataStruct getData(){
    18. MyDataStruct data;
    19. input >> data.long;
    20. input >> data.lat;
    21. return data;
    22. }
    23. private:
    24. std::ifstream &str;
    25. };
    26.  
    27. class Strings : public Base {
    28. Strings(std::ifstream &input) : Base(), str(input){}
    29. bool canHandle(const std::string &format) const{ return (format=="TEXT"); }
    30. MyDataStruct getData(){
    31. MyDataStruct data;
    32. std::string str;
    33. input >> str;
    34. data.long = atof(str.c_str());
    35. input >> str;
    36. data.lat = atof(str.c_str());
    37. return data;
    38. }
    39. private:
    40. std::ifstream &str;
    41. };
    To copy to clipboard, switch view to plain text mode 
    Then you just need to discover what the datatype is (based on some headers or whatever - that's what the "canHandle" method does) and feed the stream into an instance of a proper class.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: very simple precision question

    Quote Originally Posted by marcel View Post
    Like this:
    http://www.cplusplus.com/reference/i...precision.html

    You have to write doubles in the stream, not floats.
    Actually the link says something different:
    • On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
    • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.
    So whether the number is a float or a double doesn't have any impact on the stream.

    Here's a small example:
    Qt Code:
    1. #include <iostream>
    2. #include <iomanip>
    3.  
    4. int main( int argc, char **argv )
    5. {
    6. double d1 = 1.23456789;
    7. double d2 = 12345.6789;
    8. float f1 = 1.23456789;
    9. float f2 = 12345.6789;
    10.  
    11. std::cout << std::setprecision( 7 );
    12.  
    13. std::cout << d1 << std::endl;
    14. std::cout << d2 << std::endl;
    15. std::cout << f1 << std::endl;
    16. std::cout << f2 << std::endl;
    17.  
    18. std::cout << std::fixed;
    19.  
    20. std::cout << d1 << std::endl;
    21. std::cout << d2 << std::endl;
    22. std::cout << f1 << std::endl;
    23. std::cout << f2 << std::endl;
    24.  
    25. return 0;
    26. }
    To copy to clipboard, switch view to plain text mode 
    And the output:
    $ ./a.out
    1.234568
    12345.68
    1.234568
    12345.68
    1.2345679
    12345.6789000
    1.2345679
    12345.6787109
    As you can see, if no notation was set, "precision" means the number of meaningful digits, regardless whether we print a double or a float. After you set notation to fixed, "precision" behaves as one would expect.

  7. #7
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very simple precision question

    yep, that's what I was leaving out: "fixed". Thanks all.

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  3. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03

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
  •  
Qt is a trademark of The Qt Company.