Username:
Password:
Advanced C Programming Tutorials

Types of Data / Three Float

Subtitles of the Movie

C has three types of floating point numbers and there are two fundamental ways of implementing floating point arithmetic. One is to have floating point hardware included on the computer and if that's the case, the C compiler can choose to turn over floating point operations to it. That means, of course, the size of the floating point numbers is determined by the hardware. On the other hand, the C compiler can include a library that performs floating point operations. In this case, the floating point numbers are stored in arrays of bytes and the compiler itself is in control of size and accuracy. If it's important to your program, you should carefully study what your compiler is doing to make sure it can handle what you need it to be able to handle. But like integers, C has some declarations for different sizes and each one has a usual size. For this description, I will go by the usual size but you need to be aware that your compiler may vary. If you declare a variable as float, you declare it as being the smallest of the three possible real number sizes. It's normally 32 bits in size and carries six digits of accuracy. For example, if you were to store the value of pi in a float variable, this is as accurate as you could expect the number to be. This is quite sufficient for most uses. The next largest floating point type is a double. It is normally 64 bits in size and carries ten digits of accuracy. If you were to store the value of pi in a double variable, this is as accurate as you could expect the number to get and this is very accurate and will serve for almost everything except for some special applications that require extreme accuracy. But there even is a larger number if you need it. You can declare a variable as a long double. A long double is the newest one and the one that varies the most in size from one compiler to the next. It can be larger but it often the same size as a double and you should assume that unless you check the documentation for your compiler and it tells you differently. At one time the float type, being smaller than a double, was the faster of the two but the situation has changed and double is as fast or even faster on a modern system. As a result, the default real number type is a double, not a float. The only real reason you might want to use float values is if you need to store a lot of them on disk or if you are crowded for memory in some way. Floats are smaller and that's their only advantage. If a floating point number's accuracy is important to your application, you should study the documentation for your compiler and see what it does. In some cases you'll find options. You can use either the hardware or the floating point simulator. Your only way to be sure is to run some tests. Write up a simple program that has lots of operations of the type that you want to be able to perform and see how long it takes your compiler and how accurate it is using all the options. And you should never use floating point numbers to calculate money. It just isn't accurate enough. You would invariably lose money during the intermediate operations. You want to use the long integer forms and count pennies. That way you can have complete control over exactly what you're doing. You will find that you have to roll your own calculation methods for some things but it's the only way to be sure. Remember, there are 1700 ways to calculate interest. You should never write code that compares two floating point numbers for equality unless they got that way by one being assigned to the other. If you calculate either of the values, you'll need to decide how close they need to be to be considered equal. Define a constant of that size and test for the difference being between them no larger than that constant.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available