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

#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;
}

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:

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! :

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:

#include <iomanip>

Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.