Jump to content

  • Log In with Google      Sign In   
  • Create Account


Floating Point Constants


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 01 August 2012 - 11:57 PM

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

Sponsor:

#2 MrDaaark   Members   -  Reputation: 3551

Like
2Likes
Like

Posted 01 August 2012 - 11:59 PM

(Rusty C++) Can you use an ifdef conditional block to define them in the proper format?

#3 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 02 August 2012 - 12:03 AM

Definitely, however that would create thousands of ugly ifdefs in my application so a more elegant solution is called for.

#4 Ashaman73   Crossbones+   -  Reputation: 6956

Like
3Likes
Like

Posted 02 August 2012 - 12:08 AM

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]

#5 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
2Likes
Like

Posted 02 August 2012 - 12:09 AM

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);


#6 MrDaaark   Members   -  Reputation: 3551

Like
2Likes
Like

Posted 02 August 2012 - 12:10 AM

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, 02 August 2012 - 12:10 AM.


#7 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

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 Aardvajk   Crossbones+   -  Reputation: 5853

Like
9Likes
Like

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 MaulingMonkey   Members   -  Reputation: 1556

Like
4Likes
Like

Posted 02 August 2012 - 03:46 AM

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 Posted Image

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.

#10 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

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 RobTheBloke   Crossbones+   -  Reputation: 2325

Like
4Likes
Like

Posted 03 August 2012 - 09:36 AM

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....

#12 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 03 August 2012 - 12:29 PM

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.

#13 sundersoft   Members   -  Reputation: 216

Like
1Likes
Like

Posted 03 August 2012 - 03:26 PM

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).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS