Sign in to follow this  

Floating Point Constants

This topic is 1957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
You could do something like this:
[code]#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);[/code]

Share this post


Link to post
Share on other sites
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 Edited by Daaark

Share this post


Link to post
Share on other sites
[quote name='Daaark' timestamp='1343887823' post='4965417']
Then refactor your code so that it's not full of magic numbers?
[/quote]

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!

Share this post


Link to post
Share on other sites
[quote name='Aardvajk' timestamp='1343894354' post='4965436']
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img][/quote]
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.

Share this post


Link to post
Share on other sites
[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. Edited by James Leighe

Share this post


Link to post
Share on other sites
I used to do stuff like this, but these days I think it's a little bit pointless. What normally happens is that 99.9% of the time you're fine with floats, and then every so often you need to find a bit more precision. Having a blanket float/double type, doesn't really work that well in practice I find. It just means that 'real' will always be a float, and you'll end up using double as and when you need it. That slightly annoying reality, makes a 'real' type a bit superflous imho. Just my 2 cents....

Share this post


Link to post
Share on other sites
Very true, I wouldn't recommend it for everyone.

It's more of an experiment but it's not much more trouble than not doing it (once the changes are made to existing code) so I guess it's a bit of future proofing.

Share this post


Link to post
Share on other sites
IMO you should be using templates if you need to write code that works with any type. This allows you to use multiple types and change them later without affecting existing code. Besides float and double, it's also possible that you may use one of the integer types, complex, a quaternion type, a vector or matrix type, etc. For example, if you have a generic vector type then you can use vec<unsigned char, 4> or vec_4<unsigned char> to store image data and manipulate it in a convenient manner (although you may also have to implement casts).

Share this post


Link to post
Share on other sites

This topic is 1957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this