473,794 Members | 3,056 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
16 6267
On Wed, 13 Apr 2005 10:28:11 +0200, Karl Heinz Buchegger
<kb******@gasca d.at> wrote:
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?


imho, BigMan meant it the other way round: assign a float to a double...
Jul 23 '05 #11
ulrich wrote:

On Wed, 13 Apr 2005 10:28:11 +0200, Karl Heinz Buchegger
<kb******@gasca d.at> wrote:
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?


imho, BigMan meant it the other way round: assign a float to a double...


Sorry. Obviously I didn't pay close attention.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #12
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?
It does not. Actually, it says the opposite: "An rvalue of type float
can be converted to an rvalue of type double. The value is unchanged."
(4.6/1)
2. Where could I find more info about reducing the error of operations
on FP numbers?


Any book in line with "C++ for Scientists and Engineers". "The Art of
Computer Programming". What Karl recommended. Other good books, many
on CAD or geometry modeling.

V
Jul 23 '05 #13
On 2005-04-13 10:02:23 -0400, Victor Bazarov <v.********@com Acast.net> said:
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?
It does not.


Really?
Actually, it says the opposite: "An rvalue of type float
can be converted to an rvalue of type double. The value is unchanged."
(4.6/1)


Isn't that exactly what BigMan said? If the value is "unchanged" how
can it be less precise? Wouldn't a loss of precision count as changing
the value?
--
Clark S. Cox, III
cl*******@gmail .com

Jul 23 '05 #14
Clark S. Cox III wrote:
On 2005-04-13 10:02:23 -0400, Victor Bazarov <v.********@com Acast.net>
said:
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?

It does not.

Really?
Actually, it says the opposite: "An rvalue of type float
can be converted to an rvalue of type double. The value is unchanged."
(4.6/1)

Isn't that exactly what BigMan said? If the value is "unchanged" how can
it be less precise? Wouldn't a loss of precision count as changing the
value?


What do you want from me? So, I didn't read the question very carefully.
He got his answer. The Standard does say the assignment never results in
loss of precision, and I quoted it and gave him the paragraph number.
Jul 23 '05 #15
On 2005-04-13 11:24:19 -0400, Victor Bazarov <v.********@com Acast.net> said:
Clark S. Cox III wrote:
On 2005-04-13 10:02:23 -0400, Victor Bazarov <v.********@com Acast.net> said:
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?
It does not.

Really?
Actually, it says the opposite: "An rvalue of type float
can be converted to an rvalue of type double. The value is unchanged."
(4.6/1)

Isn't that exactly what BigMan said? If the value is "unchanged" how
can it be less precise? Wouldn't a loss of precision count as changing
the value?


What do you want from me? So, I didn't read the question very carefully.
He got his answer. The Standard does say the assignment never results in
loss of precision, and I quoted it and gave him the paragraph number.


No worries, I thought that I had missed something.

--
Clark S. Cox, III
cl*******@gmail .com

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


You probably mean accuracy.

In the typical implementation,
double precision is more precise that single [float] precision
so conversion from double to float *always* reduces precision
and conversion from float to double *always* increases precision.
Accuracy depends upon the number of significant digits [bits].
If all of the significant digits can be represented accurately
by type float, then accuracy will be maintained in the conversion
from double to float. If conversion from double to float
causes significant digits to be discarded,
the resulting inaccuracy may cause trouble.

You may be concerned about whether the conversion is *exact* or not.
IEEE floating-point specifies signals for inexact arithmetic
and you may be able trap an inexact conversion.

Consult you man pages for signal

SIGNAL(2) Linux Programmer’s Manual SIGNAL(2)
NAME
signal - ANSI C signal handling
SYNOPSIS
#include <signal.h>
typedef void (*sighandler_t) (int);
sighandler_t signal(int signum, sighandler_t handler);

and look for

SIGFPE
Some C++ compilers have options
for trapping inexact floating-point operations
but they probably won't help you much
because most floating-point operations are inexact.
Jul 23 '05 #17

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

Similar topics

15
5964
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
8028
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
12690
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
3934
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
23760
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
6748
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
9672
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
9519
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
10435
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
10213
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
10163
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
10000
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...
0
9037
agi2029
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...
0
5436
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...
3
2920
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.