473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Briefly explain why 10.12 doesn't display the value expected? printf(

2 Nibble
printf("10.10 = %.15lf\n", 10.10);
printf("10.12 = %.15lf\n", 10.12);
printf("10.15 = %.15lf\n", 10.15);
* Briefly explain why 10.12 doesn't display the value expected?
Jan 30 '21 #1
7 4294
dev7060
636 Expert 512MB
Briefly explain why 10.12 doesn't display the value expected?
What is the expected value and displaying value?
Jan 30 '21 #2
CloseShot
1 Bit
10.10 = 10.100000000000000
10.12 = 10.119999999999999
10.15 = 10.150000000000000

This is what we are getting if we print that. why 10.12 is getting a different value?
Jan 31 '21 #3
dev7060
636 Expert 512MB
Repeating fractions in the binary form may not be exactly represented as decimal point numbers. I guess this is making the difference here too.
Jan 31 '21 #4
SioSio
272 256MB
Inside the computer, decimal numbers including the decimal point are converted to binary numbers and stored in memory as follows. (32bit example)
(Integer part)
10 = 1 × 2 ^ 3 + 0 × 2 ^ 2 + 1 × 2 ^ 1 + 0 × 2 ^ 0 ⇒ 1010
(After the decimal point part)
0.12 × 2 = 0.24 ⇒ 0 for the integer part
0.24 × 2 = 0.48 ⇒ 0 for the integer part
0.48 × 2 = 0.96 ⇒ 0 for the integer part
0.96 × 2 = 1.92 ⇒ 1 for the integer part

1.92-1 = 0.92
0.92 × 2 = 1.84 ⇒ 1 for the integer part
1.84-1 = 0.84
0.84 × 2 = 1.68 ⇒ 1 for the integer part
1.68-1 = 0.68
0.68 × 2 = 1.36 ⇒ 1 for the integer part
1.36-1 = 0.36
0.36 × 2 = 0.72 ⇒ 0 for the integer part

0.72 × 2 = 1.44 ⇒ 1 for the integer part
1.44-1 = 0.44
0.44 × 2 = 0.88 ⇒ 0 for the integer part
0.88 × 2 = 1.76 ⇒ 1 for the integer part
1.76-1 = 0.76
0.76 × 2 = 1.52 ⇒ 1 for the integer part

1.52-1 = 0.52
0.52 × 2 = 1.04 ⇒ 1 for the integer part
1.04-1 = 0.04
0.04 × 2 = 0.08 ⇒ 0 for the integer part
0.08 × 2 = 0.16 ⇒ 0 for the integer part
0.16 × 2 = 0.32 ⇒ 0 for the integer part

0.32 × 2 = 0.64 ⇒ 0 for the integer part
0.64 × 2 = 1.28 ⇒ 1 for the integer part
1.28-1 = 0.28
0.28 × 2 = 0.56 ⇒ 0 for the integer part
0.56 × 2 = 1.12 ⇒ 1 for the integer part
1.12-1 = 0.12
0.12 × 2 = 0.24 ⇒ 0 for the integer part
0.24 × 2 = 0.48 ⇒ 0 for the integer part
0.48 × 2 = 0.96 ⇒ 0 for the integer part
0.96 × 2 = 1.92 ⇒ 1 for the integer part

Since it exceeds 32 bits, no further calculations are performed (truncated).

It is stored in the memory as follows.
0000 1010 .0001 1110 1011 1000 0101 0001

convert to a decimal number
10.119999945163727
Feb 1 '21 #5
dev7060
636 Expert 512MB
Sio:
It is stored in the memory as follows.
0000 1010 .0001 1110 1011 1000 0101 0001

convert to a decimal number
10.119999945163727
How does 10.119999945163727 make 10.119999999999999?

Moreover, if the same logic is applied to 10.15, binary: 00001010.00100110011001100110011, decimal representation: 10.149999976158142, but 10.15 is displayed exactly.
Feb 1 '21 #6
SioSio
272 256MB
correction.
In 64-bit environment, real constants are interpreted as double type by default.

Double precision double type
The sign part is
0
The index part is
100 0000 0010
The mantissa part is
0100 0011 1101 0111 0000 1010 0011 1101 0111 0000 1010 0011 1101
Feb 2 '21 #7
Banfa
9,065 Expert Mod 8TB
It is stored in the memory as follows.
0000 1010 .0001 1110 1011 1000 0101 0001
It most certainly is not stored in memory like that, although your latest post is more correct.

The main point is that a floating point representation is an approximation to a value and specifically it is not guaranteed that any given decimal real is precisely representable in the binary floating point form used by your computer. When you output the value you didn't ask for a fixed precision or fixed number of decimal places so the computer just gave you everything it had.

There are all sorts of problems this and the general representation of floating points introduces, for example adding small floating point numbers to large ones can introduce significant errors because the lower precision digits of the smaller number get lost when added to the larger number. The operations == and != are pretty much guaranteed to fail with floating point and you should certainly program as if they always fail (== is always false, != is always true). Even < and > may produce unexpected results at the end of a calculation because the rounding effects put the calculation on the wrong side of the line compared to doing it by hand; although personally I normally prepared to live with this).

If precision is important don't use floating point there are other options; if you decide that floating point will do then always decide on the precision you need and force your output to that precision.

If you really want to know the gritty detail then read What Every Computer Scientist
Should Know About FloatingPoint Arithmetic
.
Feb 2 '21 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dave | last post by:
Hi I m facing strange problem... I have one field char type data length 1.. It has data either 1 or 2 in all the field tht I have checked through enterprise manager. I'm running query "select...
1
by: Parveen | last post by:
Is it possible to manually provide (display,value) pairs for a combo box?? The only method I'm aware of is bind the "displaymember" and "valuemember" properties of the combobox to columns in a...
2
by: | last post by:
While I can extract the option value from the following: <Select name='Keystring' MULTIPLE> <option value='1' name='status'>Pending</option> ..... </select> I cannot seem to extract the...
1
by: satish.vell | last post by:
Hi, I have a ASP.NET page inside which I toggle the display of a field using javascript. How can I save this display value between postbacks. Here is my code: <pre>
3
by: getintanked | last post by:
<script language="javascript" type="text/javascript"> function budgetview() { if (document.form1.txtSubject.value == "Weddings") { document.getElementById(BudgetDiv).style.display='block'; } }...
3
by: =?Utf-8?B?Umljb2hEZXZlbG9wZXI=?= | last post by:
I have a class library project that uses unmanaged C dll to perform some image handling. This DLL requires me to pass in a structure containing image coordinates. In VB6, I could use a...
2
by: mervyntracy | last post by:
Hi There, I have recently started coding in asp.net (just 2 and a half days now). I am writing a simple test app that gets data from a data base and displays the value perfectly in the drop down...
1
by: bhargavi514 | last post by:
friends i need the code to retrieve value from database and display value in text area using php
1
by: jeepzhen | last post by:
Hi experts, I'm now working on a project to convert vb6 code to c#.net. I am facing a problem when using switch case in side a for loop which concatenating a constant value with for loop counter in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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,...

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.