Learn C

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Learn C as PDF for free.

More details

  • Words: 1,852
  • Pages: 8
2.5 — Floating point numbers By Alex

Integers are great for counting whole numbers, but sometimes we need to store very large numbers, or numbers with a fractional component. A floating point type variable is a variable that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226. There are three different floating point data types: float, double, and long double. A float is usually 4 bytes and a double 8 bytes, but these are not strict requirements, so sizes may vary. Long doubles were added to the language after it’s release for architectures that support even larger floating point numbers. But typically, they are also 8 bytes, equivalent to a double. Floating point data types are always signed (can hold positive and negative values). Here are some declarations of floating point numbers: view source print? 1.float fValue; 2.double dValue; 3.long double dValue2; The floating part of the name floating point refers to the fact that a floating point number can have a variable number of decimal places. For example, 2.5 has 1 decimal place, whereas 0.1226 has 4 decimal places. When we assign numbers to floating point numbers, it is convention to use at least one decimal place. This helps distinguish floating point values from integer values. view source print? 1.int nValue = 5; // 5 means integer 2.float fValue = 5.0; // 5.0 means floating point How floating point variables store information is beyond the scope of this tutorial, but it is very similar to how numbers are written in scientific notation. Scientific notation is a useful shorthand for writing lengthy numbers in a concise manner. In scientific notation, a number has two parts: the significand, and a power of 10 called an exponent. The letter ‘e’ or ‘E’ is used to separate the two parts. Thus, a number such as 5e2 is equivalent to 5 * 10^2, or 500. The number 5e-2 is equivalent to 5 * 10^-2, or 0.05. In fact, we can use scientific notation to assign values to floating point variables. view source print? 1.double dValue1 2.double dValue2 3. 4.double dValue3 5.double dValue4

= 500.0; = 5e2; // another way to assign 500 = 0.05; = 5e-2; // another way to assign 0.05

Furthermore, if we output a number that is large enough, or has enough decimal places, it will be printed in scientific notation: view source print? 01.#include 02.int main() 03.{

04.using namespace std; 05. 06.double dValue = 1000000.0; 07.cout << dValue << endl; 08.dValue = 0.00001; 09.cout << dValue << endl; 10.return 0; 11.} Outputs: 1e+006 1e-005 Precision Consider the fraction 1/3. The decimal representation of this number is 0.33333333333333… with 3’s going out to infinity. An infinite length number would require infinite memory, and we typically only have 4 or 8 bytes. Floating point numbers can only store a certain number of digits, and the rest are lost. The precision of a floating point number is how many digits it can represent without information loss. When outputting floating point numbers, cout has a default precision of 6 — that is, it assumes all variables are only significant to 6 digits, and hence it will truncate anything after that. The following program shows cout truncating to 6 digits: view source print? 01.#include 02.int main() 03.{ 04.using namespace std; 05.float fValue; 06.fValue = 1.222222222222222f; 07.cout << fValue << endl; 08.fValue = 111.22222222222222f; 09.cout << fValue << endl; 10.fValue = 111111.222222222222f; 11.cout << fValue << endl; 12.} This program outputs: 1.22222 111.222 111111 Note that each of these is only 6 digits. However, we can override the default precision that cout shows by using the setprecision() function that is defined in a header file called iomanip. view source print? 01.#include 02.#include // for setprecision()

03.int main() 04.{ 05.using namespace std; 06. 07.cout << setprecision(16); // show 16 digits 08.float fValue = 3.33333333333333333333333333333333333333f; 09.cout << fValue << endl; 10.double dValue = 3.3333333333333333333333333333333333333; 11.cout << dValue << endl; Outputs: 3.333333253860474 3.333333333333334 Because we set the precision to 16 digits, each of the above numbers has 16 digits. But, as you can see, the numbers certainly aren’t precise to 16 digits! Variables of type float typically have a precision of about 7 significant digits (which is why everything after that many digits in our answer above is junk). Variables of type double typically have a precision of about 16 significant digits. Variables of type double are named so because they offer approximately double the precision of a float. Now let’s consider a really big number: view source print? 01.#include 02. 03.int main() 04.{ 05.using namespace std; 06.float fValue = 123456789.0f; 07.cout << fValue << endl; 08.return 0; 09.} Output: 1.23457e+008 1.23457e+008 is 1.23457 * 10^8, which is 123457000. Note that we have lost precision here too! Consequently, one has to be careful when using floating point numbers that require more precision than the variables can hold. Rounding errors One of the reasons floating point numbers can be tricky is due to non-obvious differences between binary and decimal (base 10) numbers. In normal decimal numbers, the fraction 1/3rd is the infinite decimal sequence: 0.333333333… Similarly, consider the fraction 1/10. In decimal, this is easy represented as 0.1, and we are used to thinking of 0.1 as an easily representable number. However, in binary, 0.1 is represented by the infinite sequence: 0.00011001100110011… You can see the effects of this in the following program: view source

print? 1.#include 2.int main() 3.{ 4.using namespace std; 5.cout << setprecision(17); 6.double dValue = 0.1; 7.cout << dValue << endl; 8.} This outputs: 0.10000000000000001 Not quite 0.1! This is because the double had to truncate the approximation due to it’s limited memory, which resulted in a number that is not exactly 0.1. This is called a rounding error. Rounding errors can play havoc with math-intense programs, as mathematical operations can compound the error. In the following program, we use 9 addition operations. view source print? 01.#include 02.#include 03.int main() 04.{ 05.using namespace std; 06.cout << setprecision(17); 07.double dValue; 08.dValue = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1; 09.cout << dValue << endl; 10.} This program should output 1, but it actually outputs: 0.99999999999999989 Note that the error is no longer in the last column like in the previous example! It has propagated to the second to last column. As you continue to do mathematical operations, this error can propagate further, causing the actual number to drift farther and farther from the number the user would expect. Comparison of floating point numbers One of the things that programmers like to do with numbers and variables is see whether two numbers or variables are equal to each other. C++ provides an operator called the equality operator (==) precisely for this purpose. For example, we could write a code snippet like this: view source print? 1.int x = 5; // integers have no precision issues 2.if (x==5) 3.cout << "x is 5" << endl; 4.else 5.cout << "x is not 5" << endl;

This program would print “x is 5″. However, when using floating point numbers, you can get some unexpected results if the two numbers being compared are very close. Consider: view source print? 1.float fValue1 = 1.345f; 2.float fValue2 = 1.123f; 3.float fTotal = fValue1 + fValue2; // should be 2.468 4. 5.if (fTotal == 2.468) 6.cout << "fTotal is 2.468"; 7.else 8.cout << "fTotal is not 2.468"; This program prints: fTotal is not 2.468 This result is due to rounding error. fTotal is actually being stored as 2.4679999, which is not 2.468! For the same reason, the comparison operators >, >=, <, and <= may produce the wrong result when comparing two floating point numbers that are very close. Conclusion To summarize, the two things you should remember about floating point numbers: 1) Floating point numbers offer limited precision. Floats typically offer about 7 significant digits worth of precision, and doubles offer about 16 significant digits. Trying to use more significant digits will result in a loss of precision. (Note: placeholder zeros do not count as significant digits, so a number like 22,000,000,000, or 0.00000033 only counts for 2 digits). 2) Floating point numbers often have small rounding errors. Many times these go unnoticed because they are so small, and because the numbers are truncated for output before the error propagates into the part that is not truncated. Regardless, comparisons on floating point numbers may not give the expected results when two numbers are close. The section on relational operators has more detail on comparing floating point numbers.

2.6 — Boolean Values

Index

2.4 — Integers The compiler brings up a problem with setprecision()..

#include "stdafx.h" #include int main() { using namespace std; cout < < setprecision(16); float fValue; fValue = 1.3333333333333333f; cout << fValue << endl; cin.ignore(255, '/n'); cin.get(); return 0; } /* Program to know real value of fTotal */ #include #include void main() { clrscr(); double fValue; float fValue1 = 1.345f; float fValue2 = 1.123f; float fTotal = fValue1 + fValue2; // should be 2.468 if (fTotal == 2.468) { cout << "n fTotal is 2.468"; } else { cout << "n fTotal is not 2.468"; } printf("n The real value of fTotal is %0.7f", fTotal); getch(); } /* End of Pgram */ /* Program to know real value of fTotal */ #include #include void main() { clrscr(); float fValue1 = 1.345f; float fValue2 = 1.123f; float fTotal = fValue1 + fValue2; // should be 2.468

if (fTotal == 2.468) { cout << "n fTotal is 2.468"; } else { cout << "n fTotal is not 2.468"; } printf("n The real value of fTotal is %0.7f", fTotal); getch(); } /* End of Pgram */ how can i make the value a user inputs into a float? #include using namespace std; int divide() { cout << "Please enter 2 numbers" << endl; cout << "dividend \t: " ; float x; cin >> x; cout << "divisor \t: " ; float y; cin >> y; }

return x / y;

why does the float work.its a simple program to calculate the area of a triangle.but if i put the value of base as 3 and height as 3 ,i get the result as 4 instead of 4.5.here is the code.i don’t get the answer in decimal #include "stdafx.h" #include #include "add.h" #include int main() { using namespace std; cout <> b; cout <>h; float a=divide(multiply(b,h),2); //area of triangle is half into base into height cout << "area of triangle :" << a <<endl; }

When I run the following code, the values seem really wrong when output. What is going wrong here? #include #include // for setprecision() int main() { using namespace std; cout << setprecision(6) << "nprecision 6n"; float fValue; fValue = 1.22222222222222222f; cout << fValue << endl; fValue = 111111111.222222222f; cout << fValue << endl; cout << setprecision(16) << "nprecision 16n"; fValue = 1.22222222222222222f; cout << fValue << endl; fValue = 111111111.222222222f; cout << fValue << endl; return 0; }

Reply to this comment

Related Documents

Learn C#
December 2019 11
Learn C
May 2020 4
Learn Visual C++ 6
June 2020 35
Learn
June 2020 18
Learn
November 2019 37