Typedef for float or double

P

PeteUK

Hello,

I'm writing a system where I want to specify at the core via a typedef
what floating point precision I need e.g.

typedef float FPType;

or

typedef double FPType;

Other types will be based upon this, so vector and matrix classes will
take FPType as a template parameter to generate a vector/matrix of the
correct precision.

I'm left with the problem of how to initialise FPType data from a
literal. Example:

float f = 45.0f; // 'f' suffix for float
double d = 45.0; // no suffix for double

Is there a way I can abstract this away too?

Thanks,

Pete
 
J

Juha Nieminen

PeteUK said:
float f = 45.0f; // 'f' suffix for float
double d = 45.0; // no suffix for double

Is there a way I can abstract this away too?

Yes: Leave the suffix away.
 
J

Jerry Coffin

Hello,

I'm writing a system where I want to specify at the core via a typedef
what floating point precision I need e.g.

typedef float FPType;

or

typedef double FPType;

Other types will be based upon this, so vector and matrix classes will
take FPType as a template parameter to generate a vector/matrix of the
correct precision.

I'm left with the problem of how to initialise FPType data from a
literal. Example:

float f = 45.0f; // 'f' suffix for float
double d = 45.0; // no suffix for double

Leave off the suffix. If you really insist, you could use:

FPType f = (FPType)45.0;
 
P

PeteUK

  Yes: Leave the suffix away.

I'll get warnings on some compilers though I think. Not something I
want to turn off globally. Perhaps I should leave the suffix off (as
you say) and static_cast<FPType> everywhere I use a literal, or do you
think this is overkill?

Thanks for your advice so far.

Pete
 
A

Alan Woodland

PeteUK said:
I'll get warnings on some compilers though I think. Not something I
want to turn off globally. Perhaps I should leave the suffix off (as
you say) and static_cast<FPType> everywhere I use a literal, or do you
think this is overkill?

FWIW I rather like this syntax, although it doesn't 'fix' things any
better than a static_cast would of course.

typedef double FPType1;
typedef float FPType2;

//typedef BigFloat FPType3;

namespace {
const FPType1 bar = FPType1(100);
const FPType2 foo = FPType2(100.0);
}

Alan
 
P

PeteUK

FWIW I rather like this syntax, although it doesn't 'fix' things any
better than a static_cast would of course.

typedef double FPType1;
typedef float  FPType2;

//typedef BigFloat FPType3;

namespace {
   const FPType1 bar = FPType1(100);
   const FPType2 foo = FPType2(100.0);

}

Alan

Thank you - I'll bear this style in mind and see which I prefer once
the code materialises.

Pete
 
J

Juha Nieminen

Vladimir said:
Is there a case where it has a practical use?

In theory there might be cases where you don't want floats to be
promoted to doubles when performing some operation. For example:

float f1 = 1.2;
float f2 = f1 * 2.1f;

Without the 'f' suffix the "2.5" would be of type double, in which
case the 'f1' variable would be promoted to double before performing the
multiplication, after which the result is demoted back to float when
stored into 'f2'. With the suffix, all operations are (potentially)
performed on floats only.

(Of course in practice there's no saying what the compiler will do
internally. It may well promote the values to double regardless, eg. to
use the FPU to perform the multiplication.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top