problems at multiplication (Currency multiplied with float)

Discussion in 'C++' started by abdul_n_khan@hotmail.com, Dec 16, 2005.

  1. Guest

    I have a basic question related to datatype conversion. I am
    multiplying currency to float datatype.

    fltInterestRate=1.23333;
    curAmount = 91000000;

    curInterestAmount = curAmount * fltInterestRate

    curInterestAmount should have 112233030

    But i am getting 112230300 (thus I am loosing precision on the float
    variable).

    I tried to use typecasting but it didn't work and got compilation
    error.

    curAmount curInterestAmount are objects of class CCurrency whose
    implementation is part of the project.

    Any pointers in this context will be highly helpful.
    , Dec 16, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > I have a basic question related to datatype conversion. I am
    > multiplying currency to float datatype.
    >
    > fltInterestRate=1.23333;
    > curAmount = 91000000;
    >
    > curInterestAmount = curAmount * fltInterestRate
    >
    > curInterestAmount should have 112233030
    >
    > But i am getting 112230300 (thus I am loosing precision on the float
    > variable).
    >
    > I tried to use typecasting but it didn't work and got compilation
    > error.
    >
    > curAmount curInterestAmount are objects of class CCurrency whose
    > implementation is part of the project.
    >
    > Any pointers in this context will be highly helpful.


    Show us the implementation of CCurrency.

    Cheers! --M
    mlimber, Dec 16, 2005
    #2
    1. Advertising

  3. wrote:
    > I have a basic question related to datatype conversion. I am
    > multiplying currency to float datatype.


    Here is a simple rule: don't use 'float' datatype unless it is absolutely
    necessary (and if you have to ask, it's not necessary). Instead, use
    'double'. All your problems will go away.

    > [...]


    V
    Victor Bazarov, Dec 16, 2005
    #3
  4. ma740988 Guest

    || Here is a simple rule: don't use 'float' datatype unless it is
    absolutely
    || necessary (and if you have to ask, it's not necessary). Instead,
    use
    || 'double'. All your problems will go away.

    Vic, agreed with you except for cases where 'memory' becomes a concern.
    For instance, prior to performing an FFT/IFFT on data, each 10 bits
    of a 32 bit integer value gets stored as double. Here we're dealing
    with 24 bytes (double) versus 12 bytes (float) for a 32 bit integer.
    For 7 mebibytes of data that becomes hefty. In any event .........
    ma740988, Dec 16, 2005
    #4
  5. ma740988 wrote:
    > || Here is a simple rule: don't use 'float' datatype unless it is
    > absolutely
    > || necessary (and if you have to ask, it's not necessary). Instead,
    > use
    > || 'double'. All your problems will go away.
    >
    > Vic, agreed with you except for cases where 'memory' becomes a concern.


    So, why "except"? If 'memory' is a concern, it may become absolutely
    necessary to use float. Did I not leave a disclaimer in line with that?

    > For instance, [...]


    V
    Victor Bazarov, Dec 16, 2005
    #5
  6. ma740988 Guest

    || Did I not leave a disclaimer in line with that?

    Vic,

    You sure did. I might have overlooked the absolutely necessary :). I
    have a question. Sure of performing benchmarks ( which I haven't done
    yet) , is there an any truth to floats being faster argument? In my
    view the answer borders along 'hardware/implementation defined' .
    ma740988, Dec 16, 2005
    #6
  7. mlimber Guest

    ma740988 wrote:
    > Sure of performing benchmarks ( which I haven't done
    > yet) ,


    Sure of benchmarks? What does that mean?

    > is there an any truth to floats being faster argument? In my
    > view the answer borders along 'hardware/implementation defined' .


    Faster than what? As arguments to functions, you could pass a float or,
    say, an int, which on a many platforms are the same size, so there's no
    real speed gain. In many cases, hefty computations are done on *arrays*
    of floats (or whatever), and passing a pointer to float (or better, a
    reference to a vector) is the same as passing a pointer to char, speed
    wise. As far as computations go, it is hardware-dependent, but with
    floating-point hardware, even double floating-point computations can
    beat integer computations (though there is sometimes some overhead for
    switching back and forth between modes). Check your platform docs for
    more details.

    Cheers! --M
    mlimber, Dec 16, 2005
    #7
  8. Sjouke Burry Guest

    ma740988 wrote:
    > || Did I not leave a disclaimer in line with that?
    >
    > Vic,
    >
    > You sure did. I might have overlooked the absolutely necessary :). I
    > have a question. Sure of performing benchmarks ( which I haven't done
    > yet) , is there an any truth to floats being faster argument? In my
    > view the answer borders along 'hardware/implementation defined' .
    >

    A test with 16 bit c compilers i had, double was quite faster
    then float , apparently because all calc were in double and
    had to be converted to float before storage. So for these
    compilers , the only advantage for float was storage space.
    Sjouke Burry, Dec 16, 2005
    #8
  9. ma740988 wrote:
    > [...] I
    > have a question. Sure of performing benchmarks ( which I haven't done


    You mean "short of performing benchmarks"?

    > yet) , is there an any truth to floats being faster argument? In my
    > view the answer borders along 'hardware/implementation defined' .


    I've done some testing earlier in my career that made me conclude that

    (a) there is no discernible difference between 'double' and 'float' when
    passing by value;
    (b) sometimes passing a double value by a const reference may be a bit
    faster only to potentially cause some slow-down when time comes to
    access the _value_;
    (c) some compilers allow passing of arguments "in registers" which means
    that no memory access is used at all;

    ....and the last but the most important...

    (d) optimizing your program's performance by making changes of that kind
    is not worth the time spent on it.

    V
    Victor Bazarov, Dec 16, 2005
    #9
  10. Marcus Kwok Guest

    mlimber <> wrote:
    > ma740988 wrote:
    >> Sure of performing benchmarks ( which I haven't done
    >> yet) ,

    >
    > Sure of benchmarks? What does that mean?


    OP probably meant, "Short of performing benchmarks...".

    >> is there an any truth to floats being faster argument? In my
    >> view the answer borders along 'hardware/implementation defined' .

    >
    > Faster than what?


    I'm sure they meant "floats being faster than doubles".

    > As far as computations go, it is hardware-dependent, but with
    > floating-point hardware, even double floating-point computations can
    > beat integer computations (though there is sometimes some overhead for
    > switching back and forth between modes).


    Yes, it's entirely possible for the hardware developers to realize that
    doubles are much more commonly used than floats, and so have optimized
    their implementation of the hardware as such.

    --
    Marcus Kwok
    Marcus Kwok, Dec 16, 2005
    #10
  11. ma740988 Guest

    || You mean "short of performing benchmarks"?
    Thats what I meant.. Sorry!!

    Got it.. thanks for all the responses.

    Vic, as always thanks!!
    ma740988, Dec 17, 2005
    #11
    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. bd
    Replies:
    0
    Views:
    624
  2. klior

    2 Multiplied clock sync.

    klior, Jul 31, 2007, in forum: VHDL
    Replies:
    0
    Views:
    509
    klior
    Jul 31, 2007
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,540
    James Kanze
    Oct 8, 2009
  4. William Hughes
    Replies:
    13
    Views:
    1,216
    Ben Bacarisse
    Mar 15, 2010
  5. Yingjie Lan

    sequence multiplied by -1

    Yingjie Lan, Sep 25, 2010, in forum: Python
    Replies:
    2
    Views:
    266
    BartC
    Sep 26, 2010
Loading...

Share This Page