943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 842
  • C++ RSS
Nov 9th, 2009
0

g++ warning with -Wconversion flag

Expand Post »
I get this g++ warning when I use the -Wconversion flag. Anybody know how to write this "correctly" ?

Quote ...
a.cpp:15: warning: conversion to ‘float’ alters ‘double’ constant value
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. float VAR1(float VAR2)
  6. {
  7. int VAR3 = (int)(VAR2 * 1.045 * 1.55); // Result must be an int
  8. float VAR4 = (float)(VAR3 + 0.95); // Result must be float ending in .95
  9.  
  10. return VAR4; // Return a float ending in .95
  11. }
  12.  
  13. int main(void)
  14. {
  15. printf("%.2f", VAR1(25.95)); // printf a float ending in .95
  16.  
  17. return 0;
  18. }
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
raigs is offline Offline
25 posts
since Oct 2009
Nov 9th, 2009
0
Re: g++ warning with -Wconversion flag
It's because the literal value (25.95) that you are passing into your call to the function VAR1 is automatically treated as a double, but your function takes a float as a parameter. So you're getting a warning about it.

If you pass the variable like this:
C++ Syntax (Toggle Plain Text)
  1. printf("%.2f", VAR1(25.95f)); // printf a float ending in .95
The f at the end of the literal value will ensure that the value is treated as a float and not a double and should stop the warning from occurring.

in fact considering this is C++ you should really be using std::cout instead of printf in line 15 too! :
C++ Syntax (Toggle Plain Text)
  1. cout << setprecision(.2) << VAR1(25.95f);

Note: in order to use setprecision with cout, you also need to include iomanip after iostream..:
So after you've included iostream, add the following:
C++ Syntax (Toggle Plain Text)
  1. #include <iomanip>

Cheers for now,
Jas.
Last edited by JasonHippy; Nov 9th, 2009 at 9:57 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help on Project
Next Thread in C++ Forum Timeline: help :





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC