Categories:

.NET (961)

C (387)

C++ (185)

CSS (84)

DBA (8)

General (31)

HTML (48)

Java (641)

JavaScript (220)

JSP (109)

JUnit (31)

MySQL (297)

Networking (10)

Oracle (562)

Perl (48)

Perl (9)

PHP (259)

PL/SQL (140)

RSS (51)

Software QA (28)

SQL Server (5)

Struts (20)

Unix (2)

Windows (3)

XHTML (199)

XML (59)

Other Resources:

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?

Q

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?

✍: Guest

A

Most computers use base 2 for floating-point numbers as well as for integers, and just as for base 10, not all fractions are representable exactly in base 2. It's well-known that in base 10, a fraction like 1/3 = 0.333333... repeats infinitely. It turns out that in base 2, one tenth is also an infinitely-repeating fraction (0.0001100110011...), so exact decimal fractions such as 3.1 cannot be represented exactly in binary. Depending on how carefully your compiler's binary/decimal conversion routines (such as those used by printf) have been written, you may see discrepancies when numbers not exactly representable in base 2 are assigned or read in and then printed (i.e. converted from base 10 to base 2 and back again).

2015-07-03, 1011👍, 0💬

Popular Posts:

How can I implement a thread-safe JSP page? You can make your JSPs thread-safe by having them implem...

How To Use "IN OUT" Parameter Properly? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and F...

What is difference between SITP and UTP in testing ? UTP (Unit Test Plan) are done at smallest unit ...

How To Merge Cells in a Column? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells If you ...

How To Check the Oracle TNS Settings? - Oracle DBA FAQ - ODBC Drivers, DSN Configuration and ASP Con...