Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:
int main()
{
float f = 59.89F;
/* the following line caused problems */
double d = f;
/* they ended up solving it like this */
f *= 1000.0;
double dd = (double)f / 1000.0;
return 0;
}
I see what is being done but why does the latter make the situation
better?
(consider 'f' in real life to hold stock prices)
Dec 19 '06
116 35834
Dik T. Winter wrote:
In article <11************ **********@79g2 000cws.googlegr oups.com"Dilip" <rd*****@lycos. comwrites:
(consider 'f' in real life to hold stock prices)
Consider not to use floating point for stock prices. Floating point
inherently carries imprecision (especially if you want to do decimal
calculations), which you do not want with stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float. So when I reach into
the byte-stream I will have to memcpy sizeof(float) number of bytes.
Since everywhere else the system uses double to hold these prices, I
had no choice but to stuff that retreived float value into a double
causing all these problems.
2006-12-20 <11************ *********@79g20 00cws.googlegro ups.com>,
Dilip wrote:
Dik T. Winter wrote:
>In article <11************ **********@79g2 000cws.googlegr oups.com"Dilip" <rd*****@lycos. comwrites:
> (consider 'f' in real life to hold stock prices)
Consider not to use floating point for stock prices. Floating point inherently carries imprecision (especially if you want to do decimal calculations ), which you do not want with stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float.
Get a new vendor?
So when I reach into the byte-stream I will have to memcpy
sizeof(float) number of bytes. Since everywhere else the system uses
double to hold these prices, I had no choice but to stuff that
retreived float value into a double causing all these problems.
Well - if you can't get a better vendor, the best way would be to do this:
#include <math.h>
//...
float floating_price;
long int price_cents;
//...
price_cents = round(f*100.);
That way you'll actually get the value to the nearest cent, instead of
doing "voodoo magic" that happens to get the right answer in one
specific case you tested it on.
If you need tenths of a cent, use 1000. there, etc, whatever precision
you need. From that point, use the integer representation throughout
your program.
Random832 <ra****@random. yi.orgwrites:
Well - if you can't get a better vendor, the best way would be to do this:
#include <math.h>
//...
float floating_price;
long int price_cents;
//...
price_cents = round(f*100.);
For what it's worth, the round function is new in C99.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan
Dilip wrote:
Dik T. Winter wrote:
>Consider not to use floating point for stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float. So when I reach into
the byte-stream I will have to memcpy sizeof(float) number of bytes.
Since everywhere else the system uses double to hold these prices, I
had no choice but to stuff that retreived float value into a double
causing all these problems.
Putting float values into doubles probably isn't the cause of your
problems. A double will hold exactly the same value as the original
float.
In most implementations , both float and double represent numbers as
(1 + m / 2^b) * 2^e
where m is (part of) the mantissa, b is the number of bits in m, e is
the exponent, and '^' denotes exponentiation. b is 23 for floats and 53
for doubles. Given the m, b, e of a float, you get the double with
m <<= 30
b += 30
e = e
Or to put it another way,
float: 59.89 - (1 + 7311196 / 8388608) * 32
double: 59.89 - (1 + (7311196 << 30) / (8388608 << 30)) * 32
The values are identical, so the problems must be arising from what is
being done with them.
- Ernie http://home.comcast.net/~erniew
Ernie Wright wrote:
Dilip wrote:
Dik T. Winter wrote:
Consider not to use floating point for stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float. So when I reach into
the byte-stream I will have to memcpy sizeof(float) number of bytes.
Since everywhere else the system uses double to hold these prices, I
had no choice but to stuff that retreived float value into a double
causing all these problems.
Putting float values into doubles probably isn't the cause of your
problems. A double will hold exactly the same value as the original
float.
True, but irrelevent.
Mathematically
(float) (1000.0 * f) =/= 1000.0 * f
There is a small bit of rounding. So
(double)( (float) 1000.0*f)/ 1000.0 =/= f
In the present case we start out with a value just less than a multiple
of
a whole cent, and end up with a value just greater than a multiple
of a cent.
- William Hughes
Dilip wrote:
Dik T. Winter wrote:
In article <11************ **********@79g2 000cws.googlegr oups.com"Dilip" <rd*****@lycos. comwrites:
(consider 'f' in real life to hold stock prices)
Consider not to use floating point for stock prices. Floating point
inherently carries imprecision (especially if you want to do decimal
calculations), which you do not want with stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float. So when I reach into
the byte-stream I will have to memcpy sizeof(float) number of bytes.
Since everywhere else the system uses double to hold these prices, I
had no choice but to stuff that retreived float value into a double
causing all these problems.
A system that uses floating point variables to store prices should
work correctly if a price is just above or just below the
correct price (e.g for a price of 0.010001 dollars or 0.009998
dollars).
The system you use does not have this property. It needs
to be fixed. However, it appears that by management fiat you have
to use this system, and any problems are deemed yours.
A possible workaround:
Read the prices as float
Round to the nearest cent or tenth of a cent (*)
Convert to double
Add a fudge factor of 0.00000001
Now your prices will be subtly wrong, but they will be subtly wrong in
the correct direction. This should work as well as the
(double)(f*1000 .0)/1000.0
kludge, and will work for any value. (adding the fudge factor may or
may
not be necessary. It would not be necessary, but for the fact that you
have to use a broken system. There may be a price that when expressed
as a double is slightly below the true price.)
* if you don't have access to a C99 type round() function, roll your
own
(e.g. floor(100.0*dd + 0.5)/100.0) or sprintf the value and sscanf
it back
- William
Hughes
Dilip wrote:
Dik T. Winter wrote:
>"Dilip" <rd*****@lycos. comwrites:
>>(consider 'f' in real life to hold stock prices)
Consider not to use floating point for stock prices. Floating point inherently carries imprecision (especially if you want to do decimal calculations), which you do not want with stock prices.
THis isn't always possible. The market data vendor I connect to
insists that I read the stock prices as float. So when I reach
into the byte-stream I will have to memcpy sizeof(float) number of
bytes. Since everywhere else the system uses double to hold these
prices, I had no choice but to stuff that retreived float value
into a double causing all these problems.
This is inherently impossible. While some systems use IEEE float
format, many do not, so you don't even know the bit pattern coming
in. Are you sure you aren't reading and converting a text stream?
If so all you have to do is write a suitable parser for a
particular text format, say dddd.dd, where d is a digit.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Ben Pfaff wrote:
Random832 <ra****@random. yi.orgwrites:
>Well - if you can't get a better vendor, the best way would be to do this:
#include <math.h> //... float floating_price; long int price_cents; //... price_cents = round(f*100.);
For what it's worth, the round function is new in C99.
and unnecessary.
price_cents = 100 * floating_price + 0.5;
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
2006-12-20 <45************ ***@yahoo.com>,
CBFalconer wrote:
Ben Pfaff wrote:
>Random832 <ra****@random. yi.orgwrites:
>>Well - if you can't get a better vendor, the best way would be to do this:
#include <math.h> //... float floating_price; long int price_cents; //... price_cents = round(f*100.);
For what it's worth, the round function is new in C99.
and unnecessary.
price_cents = 100 * floating_price + 0.5;
I thought that was implementation-defined.
Random832 wrote:
2006-12-20 <45************ ***@yahoo.com>,
CBFalconer wrote:
Ben Pfaff wrote:
Random832 <ra****@random. yi.orgwrites:
Well - if you can't get a better vendor, the best way would be to do this:
#include <math.h> //... float floating_price; long int price_cents; //... price_cents = round(f*100.);
For what it's worth, the round function is new in C99.
and unnecessary.
price_cents = 100 * floating_price + 0.5;
I thought that was implementation-defined.
Not if price_cents is assumed to be greater than or equal to 0.
- William Hughes This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Marc Schellens |
last post by:
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?
Are there caveeats/pits?
(Apart from the range and the precision of course)
What would be the best way to do it?
thanks,
marc
|
by: Alexander Block |
last post by:
Hello newsgroup,
let's say I have a function like
template<class Type>
inline bool areEqual(const Type &a, const Type &b)
{
return ( a == b );
}
|
by: JKop |
last post by:
Does the Standard specify any minimum range or minimum
precision for the float, double and long double. I haven't
found anything in the Standard about it.
-JKop
|
by: Madan |
last post by:
Hi all,
I had problem regarding float/double arithmetic only with + and -
operations, which gives inaccurate precisions. I would like to know how the
arithmetic operations are internally handled by C# or they are hardware
(processor) dependent. Basic addition operation errors, for ex:
3.6 - 2.4 = 1.19999999999 or 1.20000000003
There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please
reply me how these operations are...
|
by: Ollie |
last post by:
Ok, As the Convert.ToDateTime has not been implimented yet, how do I convert
a double to datetime???
I have looked all over with no luck at all.
Please help me!
| |
by: illegal.prime |
last post by:
Hi all, I have a class that I would like to use to hide the data type
I'm using to store decimal point numbers. Failing all else, I will
just use object and do conversion/casts as necessary. But I was
wondering if I could use double to house all basic/primitive decimal
numbers (i.e. only the float, double and decimal types) in C#. This
type would also have to allow numerical greater than and less than
comparisons.
So I'm pretty sure...
|
by: veremue |
last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these values as Strings. Here is my code which is resulting in storing 1.4E15 not the value as a String.
String accNumber="";//declaring and initialisation
//open file and read line then store the column in accNumber
//the line read has been stored into an array String split
...
|
by: Wander |
last post by:
currently there are those functions is_float, is_double and is_real,
which do exactly the same as far as i can see
what surprises me is that $a = (float/double/real) 1; all work, while
with settype() only float and double work, and 'real' not.
now im wondering, is there any difference between those?
and if not, will they all keep existing in php6? or will php continue
with only one of them?
|
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...
|
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,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |