| | |
g++ warning with -Wconversion flag
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
I get this g++ warning when I use the -Wconversion flag. Anybody know how to write this "correctly" ?
•
•
•
•
a.cpp:15: warning: conversion to ‘float’ alters ‘double’ constant value
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; float VAR1(float VAR2) { int VAR3 = (int)(VAR2 * 1.045 * 1.55); // Result must be an int float VAR4 = (float)(VAR3 + 0.95); // Result must be float ending in .95 return VAR4; // Return a float ending in .95 } int main(void) { printf("%.2f", VAR1(25.95)); // printf a float ending in .95 return 0; }
0
#2 27 Days Ago
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:
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! :
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:
Cheers for now,
Jas.
If you pass the variable like this:
C++ Syntax (Toggle Plain Text)
printf("%.2f", VAR1(25.95f)); // printf a float ending in .95
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)
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)
#include <iomanip>
Cheers for now,
Jas.
Last edited by JasonHippy; 27 Days Ago at 9:57 am.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
![]() |
Similar Threads
- LNK2001 and LNK2019 problem! (C++)
- "warning: NULL used in arithmetic" (C++)
- Compiler warning for = vs. == ? (C++)
- help wanted (C)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- complex declarations & other doubts (C++)
Other Threads in the C++ Forum
- Previous Thread: Need Help on Project
- Next Thread: help :
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





