#1 Members - Reputation: 217
Posted 01 August 2012 - 11:57 PM
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!
#4 Members - Reputation: 4742
Posted 02 August 2012 - 12:08 AM
[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]
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#6 Members - Reputation: 3515
Posted 02 August 2012 - 12:10 AM
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
Edited by Daaark, 02 August 2012 - 12:10 AM.
#7 Members - Reputation: 217
Posted 02 August 2012 - 12:39 AM
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!
#8 Members - Reputation: 2414
Posted 02 August 2012 - 01:59 AM
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:
typedef float real; // or double real x = real(23.0);
is quite sufficient.
Edited by Aardvajk, 02 August 2012 - 02:00 AM.
#9 Members - Reputation: 1554
Posted 02 August 2012 - 03:46 AM
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.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
#10 Members - Reputation: 217
Posted 02 August 2012 - 06:30 AM
typedef float real; // or double real x = real(23.0);
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.
Edited by James Leighe, 02 August 2012 - 06:31 AM.
#11 Members - Reputation: 1364
Posted 03 August 2012 - 09:36 AM
#13 Members - Reputation: 216
Posted 03 August 2012 - 03:26 PM






