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. 10 2403
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 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
|| 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 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
|| 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 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
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.
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
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
|| You mean "short of performing benchmarks"?
Thats what I meant.. Sorry!!
Got it.. thanks for all the responses.
Vic, as always thanks!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J.K. Becker |
last post by:
Hi there,
I am trying to multiply doubles with floats (actually I tried every
possible combination by now) and it never works (well, it does something
but it is always wrong). I have no idea...
|
by: anagai |
last post by:
hi
Isnt it true that floating type can generate rounding errors if the
values are rounded to like the 10th decimal. Since currency is rounded
only to the 2nd decimal why must i worry about using...
|
by: Andy |
last post by:
Hi,
I don't know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This...
|
by: Ralf Hildebrandt |
last post by:
Hi all!
First of all: I am a C-newbie.
I have noticed a "strange" behavior with the standart integer
multiplication. The code is:
void main(void)
|
by: jdcrief |
last post by:
Complier: Visual C++ 2005 Express Edition
The program I wrote will compile and execute, but the output is always
the same, no matter what number is entered in for the radius of the
circle. ...
|
by: One |
last post by:
hi group -
I had this in a function - so I've just moved this out of the function
for readabilty.
Can someone please tell me WHY does this not return the result of :
n1 times n2
As I said...
|
by: tararreb |
last post by:
#include<stdio.h>
/*This line is standard input output, # is directive, include is keyword, and stdio.h is header file*/
#include<stdlib.h>
/*This line is standard input output, # is directive,...
|
by: clairelee0322 |
last post by:
This program doesn't run because some problem with the reduce fraction.
My reduce fraction is all right but when I tried to put that in this program, It has some errors.
Or maybe the switch menu is...
|
by: Sozos |
last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help.
#define index(i,j,power) (((i)<<(power))+(j))
void...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |