473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems at multiplication (Currency multiplied with float)

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

fltInterestRate =1.23333;
curAmount = 91000000;

curInterestAmou nt = curAmount * fltInterestRate

curInterestAmou nt 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 curInterestAmou nt 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
10 2429
abdul_n_k...@ho tmail.com wrote:
I have a basic question related to datatype conversion. I am
multiplying currency to float datatype.

fltInterestRate =1.23333;
curAmount = 91000000;

curInterestAmou nt = curAmount * fltInterestRate

curInterestAmou nt 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 curInterestAmou nt 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
ab**********@ho tmail.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
|| 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
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
2207
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 what it is and where to look for help, maybe some of you know? double=float*double; (or every possible combination of it). An example: 0.3 * 0.7 would result in 1.7 (with lots more digits). Anyone any idea?
1
3856
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 float for currency calculations? decimal is slow and memory hog. is it true that mysql calculates internally using double float?
54
8368
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 creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
9
8001
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)
38
5907
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. Someone please help! I am extremely new at this, as you can probably tell. What am I missing with the code below? #include "stdafx.h"
13
3332
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 - I took it out of the function just for readability : In my php file :
7
3768
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, include is keyword, and stdlib.h is standard library defininition*/ #include<math.h> /*This line is standard input output, # is directive, include is keyword, and math.h is mathematical declarations*/ main (void)
1
2543
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 wrong? because I put another swith menu in the main switch menu. I guess the problem is In the division - fraction section the error says something like cases are skipped by something.. I don't remember because I only get the chances to use C++...
1
9163
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 recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
0
8969
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.