problems at multiplication (Currency multiplied with float)

A

abdul_n_khan

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

mlimber

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
 
V

Victor Bazarov

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
 
M

ma740988

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

Victor Bazarov

ma740988 said:
|| 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
 
M

ma740988

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

mlimber

ma740988 said:
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
 
S

Sjouke Burry

ma740988 said:
|| 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.
 
V

Victor Bazarov

ma740988 said:
[...] 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
 
M

Marcus Kwok

mlimber said:
Sure of benchmarks? What does that mean?

OP probably meant, "Short of performing benchmarks...".
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.
 
M

ma740988

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

Got it.. thanks for all the responses.

Vic, as always thanks!!
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top