C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2003, 05:57 PM
Obscurity Obscurity is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Monrovia, Los Angeles County, California
Posts: 46 Obscurity User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 27 sec
Reputation Power: 11
A rundown of the Float Datatype

I'm not sure if this belongs here, but I've been working with C++ and my tutorial/book is very very helpful yet, it hasn't explained the float datatype. I know char and int, but we've used float with almost no explenation. If someone could help me out, I would be very grateful. Thankyou.

- Obscurity

Reply With Quote
  #2  
Old June 5th, 2003, 11:06 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
char ch='a';
cout<<'a'<<endl;

int number= 5;
cout<<number/4<<endl; //answer is 1

What if I want to use numbers with decimal points, i.e floating point numbers, for more accuracy:

float number = 5.0f; //f for float
cout<<number/4<<endl; //answer is 1.25;

Last edited by 7stud : June 5th, 2003 at 11:14 PM.

Reply With Quote
  #3  
Old June 5th, 2003, 11:33 PM
Obscurity Obscurity is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Monrovia, Los Angeles County, California
Posts: 46 Obscurity User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 27 sec
Reputation Power: 11
Ah thanks alot man, I owe you one. Do you know where float ends though (such as char 128 -127 and int 32767 -32767)

Reply With Quote
  #4  
Old June 6th, 2003, 01:07 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
Quote:
Originally posted by Obscurity
Ah thanks alot man, I owe you one. Do you know where float ends though (such as char 128 -127 and int 32767 -32767)

float is floating-point, so it's a fractional part out to so many decimals (binaries, actually) and its exponent ranges from +/- whatever.

If you are using Visual C++, the ranges should be in the help file. I'm not sure what the ranges are for gcc.

BTW, a greater precision floating-point type is double, which is usually the default floating-point type.

EDIT:
From VC++6 help file:
Quote:
Table 2.4 Sizes of Fundamental Types

Type Size
char, unsigned char, signed char 1 byte
short, unsigned short 2 bytes
int, unsigned int 4 bytes
long, unsigned long 4 bytes
float 4 bytes
double 8 bytes
long double1 8 bytes

Maximum representable floating-point number.
FLT_MAX 3.402823466e+38F
DBL_MAX 1.7976931348623158e+308
LDBL_MAX 1.7976931348623158e+308

Minimum positive value.
FLT_MIN 1.175494351e–38F
DBL_MIN 2.2250738585072014e–308
LDBL_MIN 2.2250738585072014e–308

VC++ 1.52 help has a much better page. I'll edit it into here tomorrow.

Last edited by dwise1_aol : June 6th, 2003 at 01:28 AM.

Reply With Quote
  #5  
Old June 6th, 2003, 08:42 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
It should be noted that the ranges for float are not exactly restricted in the same manner as integers... A 2 byte signed integer can be from -32,768 .. 32,767, and be any integer within these ranges. A float cannot simply be any float between the ranges of a float, because the floating point data type only has so much precision. Think about a float as a number like so:
1.957934 * 10 ^ 45
or
1.957934 E 45
A certain number of bits are used to store the exponent (45 in this case) and a certain number of bits are used to store the mantissa (1.957934 in this case). The exponent has a limited range, which is basically what sets the limits of a floating point data type. BUT, the mantissa also has its limits. If you attempt to add 1.0 to the number above, you'll get the same number, because only so many digits of the mantissa are stored. For example:

1.957934 E 45 + 1 = 1.957934000000000000000000000000000000000000001 E 45 (no I didn't count the number of 0s, so it is probably off by a few)

since we can only store 7 or 8 digits of the mantissa in a float data type, this is rounded to:

1.957934 E 45

and thus, this is what is stored for the result of the addition.

Also, since floating point types are stored in binary format, some non repeating decimal numbers such as 0.1 cannot be stored accurately in a float, since they are repeating in binary. 0.1 decimal = 0.0001100110011.... binary. We can only store so many binary digits, and the rest are cut off. It's similar to attempting to store 1/3 which is 0.33333333333... with a limited number of decimal digits. Eventually, we have to cut off the rest, and we do not accurately store 1/3.

Try making a program that keeps adding 0.1 to itself (and keeps adding 1 to an integer variable), and see if the float remains 1/10th the size of the integer... it won't. Accumulative error is to blame, since you are not actually adding 0.1 every time.

Hope this helps.

Last edited by Jason Doucette : June 6th, 2003 at 08:45 AM.

Reply With Quote
  #6  
Old June 6th, 2003, 08:46 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
not sure how spot on this is or not, but it'll at least roughly work out the max and min vals of float:

Code:
#include <stdio.h>

main()
{
	float i;
	float j;
	
	j=i=0.0;
	while (++i>j)
		j++;
	printf("float max: %f\n", j);
	
	j=i=0.0;
	while (--i<j)
		j--;
	printf("float min: %f\n", j);
}


also there's a header file called limits.h that defines maxes and mins, but double and float aren't in there, not my one, for some reason.

Reply With Quote
  #7  
Old June 6th, 2003, 10:03 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
Here is the Visual C++ 1.52 help page on data types:
Code:
 Data Types

C/C++ recognizes the types shown in the table below.
-------------------------------------------
Type Name	Bytes	Other Names	Range of Values

int                            *              signed,                    	System dependent
                                                signed int

unsigned int            *              unsigned                  System dependent

char                         1              signed char             -128 to 127

unsigned char         1	none                       0 to 255

short                       2               short int,                 -32,768 to 32,767
                                                signed short int	 

unsigned short        2              unsigned short int   0 to 65,535

long                         4              long int,                    -2,147,483,648 to 2,147,483,647
                                                signed long int	

unsigned long         4               unsigned long int     0 to 4,294,967,295

enum                       2              none                        -32,768 to 32,767

float                        4               none                        3.4E +/- 38 (7 digits)

double                    8               none                        1.7E +/- 308 (15 digits)

long double           10              none                        1.2E +/- 4932 (19 digits)

Signed and unsigned are modifiers that can be used with any integral type. 
The char type is signed by default, but you can specify /J to make it unsigned by default.

The int and unsigned int types have the size of the system word.
This is two bytes (the same as short and unsigned short) on MS-DOS and 16-bit versions of Windows. 
However, portable code should not depend on the size of int.

Please also note that the format of floating-point numbers is specified in IEEE 754; http://research.microsoft.com/~holl.../ieeefloat.html .

If you are going to inspect your floating-point numbers on the bit level (eg, in hex) and you are using an Intel machine, keep in mind that Intel machines are little-endian, such that the bytes of a multi-byte value are in reverse order, with the least significant byte first.

Last edited by dwise1_aol : June 6th, 2003 at 10:05 AM.

Reply With Quote
  #8  
Old June 8th, 2003, 04:45 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
The range of values for floating point types (float, double, long double) is implementation defined, so it will vary between different compilers. To find out the range for your compiler output the max and min values:

Code:
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    cout << "Maximum floating point values" << endl;
    cout << "=============================" << endl;
    cout << "Max float: " << numeric_limits<float>::max() << endl;
    cout << "Max double: " << numeric_limits<double>::max() << endl;
    cout << "Max long double: " << numeric_limits<long double>::max() << endl;
    cout << "\nMinimum floating point values:" << endl;
    cout << "=============================" << endl;
    cout << "Min float: " << numeric_limits<float>::min() << endl;
    cout << "Min double: " << numeric_limits<double>::min() << endl;
    cout << "Min long double: " << numeric_limits<long double>::min() << endl;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > A rundown of the Float Datatype

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap