This explanation was written keeping in mind the confusion caused by the fact that one has two different types of floating point numbers at their disposal. Read below to find out what you could be missing out on in your C programs.
Firstly, you must know that a "Floating point number" refers to a number with a decimal place followed by a number or numbers. Also there are what some may refer to as "Two types of floating point number types." Although they are more or less the same with different features, as they do the same thing in slightly different ways.
What differences will I notice when I opt for “double” as opposed to “float” or vice versa:
- By using the "float" function to create a floating point number you are creating a variable that is capable of storing a numerical data value (number) within itself with up to 7 numbers after the decimal point. Wow! Accuracy to seven points, that sounds crazy. This is the most commonly used floating point variable type.
- By using the "double" function to create a floating point number, you are creating a variable that is capable of storing a numerical data value inside itself with not seven, but 15 numbers after the decimal point. Which means you can have up to 15 point of accuracy on a single integer. This type of floating point number variable is used more rarely, in applications that require utmost accuracy.
Perhaps you would like to know about signed and unsigned integers in C while you are here? If so you must see the explanation linked below:
Hopefully this explanation has removed your once-hazy understanding of the different types of "Floating point numbers" in C. Enjoy your newfound C language knowledge.
This post was written to elaborate on any curiosities you may have on the differences between signed and unsigned integer variables in C. Read below to find out exactly what they are and why you should be using the both of them.
First and foremost, a signed integer can contain numbers in a range built on the same amount of numbers as that of an unsigned integer’s range, although they are at a different end spectrum.
By creating a variable using the "int" function, you have created a signed integer variable. This default can be reversed by using the "unsigned int" function. Why would you want to do that you ask? Well that is simple:
- A signed integer variable can contain a numerical data value within the range of -32768 to 32767.
- An unsigned variable can contain a numerical data value within the range of 0 to 65535.
Now why would that be useful? You may be wondering. It is quite simple. For a vast majority of applications you will use the default signed integer variable type. Which is no less than satisfactory; unless you wish to create a variable that will be used to contain a number larger than 32767 or a variable that will force a positive number.
A signed integer variable can be very volatile if used in incorrect situations. Unlike a floating point variable, the signed integer variable will become a negative number if its range is exceeded, if the range is exceeded enough, it may even become positive again. This creates a potential for much inaccuracy. For example, if you were to use the "scanf()" function to allow the user at the keyboard to enter a value for a signed integer and he or she decided to enter the value 32768 for example, which is one number above the allowed maximum for a signed integer variable, then the value of the signed integer variable will become something crazy. In my case it returned "909586995".
When using an unsigned integer, you have a higher range in positive numbers, although you have no option for negative numbers. The range for an unsigned integer is, as stated before, 0 to 65535. Which is much more practical for a majority of more technical applications that require avoidance of negative numbers.
Now that you have indulged your mind in the wonderful language that is C. Perhaps you would like to head over to the explanation on the different types of “Floating point numbers” linked below:
I do so hope that this enhanced your knowledge on the use of signed and unsigned integer variables in the C language. Enjoy your newfound C integer variable knowledge.
This post contains the code I wrote recently, of which is used to calculate the amount of money owed to the bank when you get the dreaded “You are assessed for street repairs: $40 per house and $115 per hotel” community chest card during an otherwise spectacular game of everybody’s favourite board game Monopoly. This tiny source, when compiled, will allow you to enter the number of currently owned houses and hotels and it will subsequently return the amount of money you owe the bank in dollars($). This is not so much a practical application, although it is a potential learning resource for people new to the great language C.
The code for “Monopoly Hack.c” is below:
* Monopoly Hack.c
* Created on: 18/02/2011
* Author: Dillon Chaffey
int houses, hotels, total;
printf(“Enter your current number of houses: “);
printf(“Enter your current number of hotels: “);
printf(“You owe the bank a total of $%d.\n”, total);
This is a rather useless program in its entirety, although by using a modified version of the source above within another program may prove very useful.
I hope to see this source utilized within another program some time soon. Enjoy your newfound C knowledge.
This post is the first of my new section “Chaff’s Code”, and I hope you will find it useful as an addition to a current project or perhaps inspiration toward hitting that keyboard again and writing that program you have had stuck in your head for what feels like months.
The entire source is quite small, although very efficient for demonstration. The source is below:
* Integer Vs. Floating Point Number Demonstration.c
* Created on: 19/02/2011
* Author: Dillon Chaffey
int int_num1, int_num2, int_num3; /* Line to the left declares three integers by using “int” and then naming the integer variables beside it, which are only allowed to be whole numbers, and are separated by commas of course and ended with a semicolon */
float float_num1, float_num2, float_num3; /* Declares floating point numbers (integer variables with a decimal, i.e 1.39 as opposed to an int which would support whole numbers only, separated by commas) */
int_num1 = 30 / 10; /* Both the divisor and dividend are integers. — Declares that the integer variable “int_num1” is now equal to (30 divided by ten), which is 3 */
float_num1 = 30 / 10;
int_num2 = 46.0 * 75; /* Only the dividend is an integer, the divisor is a float (floating point number) */
float_num2 = 46.0 * 75;
int_num3 = 40 / 8.0; /* Declares the equation “40 divided by 8.0”, which equals ‘5.0’ */
float_num3 = 40 / 8.0;
printf(“The integer representation of the equation ’30 / 10′ is: %d\n”, int_num1);
printf(“The float (floating point) representation of the equation ’30 / 10′ is: %f\n”, float_num1);
printf(“The integer representation of the equation ‘46.0 * 75’ is: %d\n”, int_num2);
printf(“The float representation of the equation ‘46.0 * 75′ is: %f\n”, float_num2);
printf(“The integer representation of the equation ’40 / 8.0′ is: %d\n”, int_num3);
printf(“The float representation of the equation ’40 * 8.0’ %f\n”, float_num3);
The above code was written by myself in a very short period of time, although the potential assistance it could render to a person new to the C language is limitless, assuming they are interested in the mathematical side of programming.
I hope that this source will help you and others in some way or another. Enjoy your newfound C knowledge.