Typedef for float or double

Discussion in 'C++' started by PeteUK, Jul 19, 2009.

  1. PeteUK

    PeteUK Guest

    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
    PeteUK, Jul 19, 2009
    #1
    1. Advertising

  2. PeteUK wrote:
    > 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.
    Juha Nieminen, Jul 19, 2009
    #2
    1. Advertising

  3. PeteUK

    Ron Guest

    On Jul 19, 10:07 am, Juha Nieminen <> wrote:
    > PeteUK wrote:
    > > 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.


    Juha is right here. There's no practical use for it in this case.
    Ron, Jul 19, 2009
    #3
  4. PeteUK

    Jerry Coffin Guest

    In article <2894e076-6254-4216-adcc-a74b0a389733
    @c1g2000yqi.googlegroups.com>, says...
    >
    > 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;

    --
    Later,
    Jerry.
    Jerry Coffin, Jul 19, 2009
    #4
  5. PeteUK

    PeteUK Guest

    On 19 July, 15:07, Juha Nieminen <> wrote:
    > PeteUK wrote:
    > > 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.


    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
    PeteUK, Jul 19, 2009
    #5
  6. PeteUK wrote:
    > On 19 July, 15:07, Juha Nieminen <> wrote:
    >> PeteUK wrote:
    >>> 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.

    >
    > 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
    Alan Woodland, Jul 19, 2009
    #6
  7. PeteUK

    PeteUK Guest

    On 19 July, 18:54, Alan Woodland <> wrote:
    > PeteUK wrote:
    > > On 19 July, 15:07, Juha Nieminen <> wrote:
    > >> PeteUK wrote:
    > >>> 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.

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


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

    Pete
    PeteUK, Jul 19, 2009
    #7
  8. Ron wrote:
    > On Jul 19, 10:07 am, Juha Nieminen <> wrote:
    >> PeteUK wrote:
    >>> 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.

    >
    > Juha is right here. There's no practical use for it in this case.



    Is there a case where it has a practical use?
    Vladimir Jovic, Jul 20, 2009
    #8
  9. Vladimir Jovic wrote:
    > 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.)
    Juha Nieminen, Jul 20, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    4
    Views:
    16,513
  2. Sydex
    Replies:
    12
    Views:
    6,486
    Victor Bazarov
    Feb 17, 2005
  3. bd
    Replies:
    0
    Views:
    621
  4. oor
    Replies:
    0
    Views:
    1,346
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,539
    James Kanze
    Oct 8, 2009
Loading...

Share This Page