Floating Point Constants

Started by
11 comments, last by sundersoft 11 years, 8 months ago
(in c++)

In my code I have a special type that allows me to switch between 'float' and 'double' for at least some portions of the code.

The problem is constants like '0.015' are default double, and generate tonnes of warnings when I switch to 'float' mode. Because of expressions like 'floatn = 1.5'. However, if I append an 'f' to the end of every constant then I could be removing some of the accuracy benefits of doubles in double mode.

What should I do to get rid of these warnings and still allow me to have the benefit of doubles when I compile it with 'floatn' typedef'd to 'float'?

Thank!
Advertisement
(Rusty C++) Can you use an ifdef conditional block to define them in the proper format?
Definitely, however that would create thousands of ugly ifdefs in my application so a more elegant solution is called for.
You could explicitly cast it like:
[source lang="cpp"]#ifdef FLOAT_SUPPORT==1
#define MyFloat(x) float(x)
#else
#define MyFloat(x) double(x)
#end


#define MyConst MyFloat(1.23)



[/source]
You could do something like this:
#define USE_FLOAT

#ifdef USE_FLOAT
typedef float Real;
#define CREAL(x) x ## f
#else
typedef double Real;
#define CREAL(x) x
#endif

const Real PI = CREAL(3.14);
Then refactor your code so that it's not full of magic numbers?

Create a function that returns the proper data type, and use an ifdef to define it? Feed it a string, and it will return either a float or a double, depending on your definitions.

edit: Wow, ninja'd

Then refactor your code so that it's not full of magic numbers?


I try not to use magic numbers, the vast majority of the warnings come from initialization and simple arithmetic.


Thanks everyone I'll give your ideas a shot, but I'm still open to more suggestions!

The vast majority of the warnings come from initialization and simple arithmetic.


Well then it seems highly unlikely you need to worry about loss of precision when converting from float to double, unless your simple arithmetic involves numbers like 23423.4234234098029384233409583405 :)

Incidentally, if you've already defined an alias for the type, there is no need for a further macro for the cast, given C++'s constructor cast syntax:

[source]
typedef float real; // or double

real x = real(23.0);
[/source]

is quite sufficient.

Well then it seems highly unlikely you need to worry about loss of precision when converting from float to double, unless your simple arithmetic involves numbers like 23423.4234234098029384233409583405 smile.png

Of course, do note that simple decimal expressions like "0.3" have an infinite number of digits in binary fraction form and will suffer additional rounding if coerced to float.
[source]
typedef float real; // or double

real x = real(23.0);
[/source]

I'm not sure why that didn't occur to me last night...

And yeah I'm being anal about precision even though its hardly an issue but it helps for arithmetic with pi or simply 1/3 like the above poster said.

This topic is closed to new replies.

Advertisement