| | |
g++ warning with -Wconversion flag
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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 Nov 9th, 2009
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; Nov 9th, 2009 at 9:57 am.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
![]() |
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 :
Views: 285 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





