By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

problems at multiplication (Currency multiplied with float)

P: n/a
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 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
abdul_n_k...@hotmail.com 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

Dec 16 '05 #2

P: n/a
ab**********@hotmail.com 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
Dec 16 '05 #3

P: n/a
|| 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 .........

Dec 16 '05 #4

P: n/a
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
Dec 16 '05 #5

P: n/a

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

Dec 16 '05 #6

P: n/a
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

Dec 16 '05 #7

P: n/a
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.
Dec 16 '05 #8

P: n/a
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
Dec 16 '05 #9

P: n/a
mlimber <ml*****@gmail.com> 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
Dec 16 '05 #10

P: n/a

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

Got it.. thanks for all the responses.

Vic, as always thanks!!

Dec 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.