473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loss of precision assigning floating point values?

How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?

Jul 23 '05 #1
16 6263
BigMan wrote:
How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?


Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V
Jul 23 '05 #2

Victor Bazarov wrote:
BigMan wrote:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?


Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V


double d = ...;
float f = (float)d;
bool loss = (d != (double)f);

?

Regards,
Bogdan Sintoma

Jul 23 '05 #3
Bogdan Sintoma wrote:
Victor Bazarov wrote:
BigMan wrote:
How can I check if assignment of a float to a double (or vice
versa)
will result in loss of precision?


Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V

double d = ...;
float f = (float)d;
bool loss = (d != (double)f);

?


I think this measures if the assignment _has_resulted_ in a loss of
precision, not if it _will_ result, don't you agree?

V
Jul 23 '05 #4

Victor Bazarov wrote:
Bogdan Sintoma wrote:
Victor Bazarov wrote:
BigMan wrote:

How can I check if assignment of a float to a double (or vice


versa)
will result in loss of precision?
Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

V

double d = ...;
float f = (float)d;
bool loss = (d != (double)f);

?


I think this measures if the assignment _has_resulted_ in a loss of
precision, not if it _will_ result, don't you agree?

Agree :), but those are the implementation details of 'some' function
that check if the conversion of a double into a float will result in
loss of precision ;).

Bogdan

Jul 23 '05 #5
On Tue, 12 Apr 2005 09:27:22 -0400 in comp.lang.c++, Victor Bazarov
<v.********@com Acast.net> wrote,
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.


What then is wrong with the following reasoning? If, after assigning
the double to a float, the float compares equal to the double, then no
precision was lost, otherwise it was.

Of course I would never suggest using float if you can afford double.

Jul 23 '05 #6
David Harmon wrote:
On Tue, 12 Apr 2005 09:27:22 -0400 in comp.lang.c++, Victor Bazarov
<v.********@com Acast.net> wrote,
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.

What then is wrong with the following reasoning? If, after assigning
the double to a float, the float compares equal to the double, then no
precision was lost, otherwise it was.


Nothing is wrong except that the OP wanted to check if assignment "will
result in loss", not if it "has resulted in loss".
Of course I would never suggest using float if you can afford double.


Yes, but for large quantities of data, like coordinates that are read from
a file and are known to never have more than 5 digits, say, there is no
need to try to "afford double". And often storing only floats saves quite
a significant amount of memory. Intermediate operations on those values
can be performed in doubles, storing resulting values back could mean
losing some precision... There are many ways to reduce the error of math
operations on FP numbers, but they are not the subject of this thread, so
I'll shut up for now.

V
Jul 23 '05 #7
On Tue, 12 Apr 2005 12:24:33 -0400 in comp.lang.c++, Victor Bazarov
<v.********@com Acast.net> wrote,
What then is wrong with the following reasoning? If, after assigning
the double to a float, the float compares equal to the double, then no
precision was lost, otherwise it was.


Nothing is wrong except that the OP wanted to check if assignment "will
result in loss", not if it "has resulted in loss".


If you do it using a scratch variable, the test predicts whether it
will result in loss when you do it with the real thing.

Jul 23 '05 #8
1. Does the C++ standard say that assigning a float to a double never
results in loss of precision? If so, where does it say so?
2. Where could I find more info about reducing the error of operations
on FP numbers?

Jul 23 '05 #9
BigMan wrote:

1. Does the C++ standard say that assigning a float to a double never
results in loss of precision? If so, where does it say so?
No. How could it.
Assume: sizeof( double ) == 8, sizeof( float ) == 4
How can one pack 8 bytes into 4 without loosing anything?
2. Where could I find more info about reducing the error of operations
on FP numbers?


Read eg. "What Every Computer Scientist Should Know About
Floating-Point Arithmetic"
http://docs-pdf.sun.com/800-7895/800-7895.pdf

HTML Version
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Having said that, here is my standard advice:

Until you know what you do and have the knowledege to do
it, better forget that there is a data type float. Always
use double instead, unless
* you know what you are heading at
* you have the knowledge and are willing to fight that beast
* you have a very very very very good reason to use float.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #10

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

Similar topics

15
5962
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be set from Python so that 3.4 yield 3.4? Thanks, Károly
24
8027
by: Philipp | last post by:
Hello (not sure this is the right forum for that question so please redirect me if necessary) How can I know how many double values are available between 0 and 1? On my machine (pentium 3) I get a sizeof(double) = 8 Is the distribution of the double values in a fixed range (eg here between 0 and 1) uniform? ie same number of values in the range [0.0 ; 0.1[ than in the range [0.9 ; 1.0[
5
7997
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to add the commas, I convert it to a string (specifically a char rather than an actual string object) using the gcvt function as shown below. char amt; gcvt(amount,50,amt);
4
2245
by: jonas.email | last post by:
Im am doing a major project and after a long time debugging i found this rather funny (=annoying) bug. In its simple form, this is the main problem: int main(void) { float f1 = 0.3f; float f2 = 0.1f; printf("%f, %.10f, %f, %.10f\n", f1, f1, f2, f2); if(f1 == 3*f2)
21
12689
by: syntax | last post by:
hi, i need to get high presion float numbers. say, i need pi = 22/7.0 = 3.142857....(upto 80 digits) is it possible ? does gcc/g++ compiler can give such type of high precision?? plz GIVE A SMALL CODE HOW CAN I ACHIEVE THAT ? which way, i have to
15
3933
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
3
23758
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...
15
2938
by: giff | last post by:
Hi all, I have a doubt, I'll try to expose it to you as clearly as I can, maybe it is not completely in topic, sorry about that. I have a few vectors of doubles (output of some calculations) and I check the norm and the dot product between couples of them before storing them in a txt file, obtaining the expected results. The vectors are orthonormal so the norm=1 and the dot product gives
137
6727
by: mathieu.dutour | last post by:
Dear all, I want to do multiprecision floating point, i.e. I want to go beyond single precision, double precision and have quadruple precision, octuple precision and the like, and possibly with high speed. What would be the possible alternatives? Thanks for any help
0
9643
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
9480
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
10315
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9946
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
7494
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...
0
6737
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();...
0
5379
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...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.