Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusion about the range of certain data type

Status
Not open for further replies.

Wuu

Technical User
Nov 18, 2007
33
0
0
US
Hi,

I am a C++ beginner, I have received some prompt replies at the site. So here I am again.

As the subject indicates, it is about the range of some numerical data type. I have a fair understanding about the range of char, short, int, and long, but I am confused about float, double, or long double.

For example, in a table ( I encountered, float has a range of 3.4e +/-38(7 digits).

Question 1
Integers have signed and unsigned, so I am able to use negative numbers. But how do I use negative float numbers, such as -7.456 or -0.023, if the range doesn't even include negative numbers?

Question 2
What does the "7 digits" inside the parentheses refer to? the range is (1.06730515 × 10^-16) to )1.08310168 × 10^17).


Thanks
 
Floating point numbers are always signed, so you can have positive or negative values.
Code:
#include <iostream>
using namespace std;

int main()
{
	double num = -3.14;
	cout << "neg pi = " << num << endl;
	return 0;
}

The 7-digits in parenthesis is the precision. A float can have a number up to 38 digits long, but only 7 of those numbers are accurate, the rest is unknown. So pi in a float can only be accurate up to: 3.141593, but a double can show it as: 3.14159265358979.
 
Hi,

cpjust, thanks so much for your reply. Still confused though.

Quote:
"Floating point numbers are always signed, so you can have positive or negative values."

But this range, "3.4e +/-38(7 digits)" for float is copied directly from the online tutorial (see link above), and it would shock me if it is wrong. And the both extremes of this range are postive, so where does the negative numbers fit in?

Or your meant the float's range is ACTUALLY (3.4e +/-38) AND (-3.4e +/-38)? Not just (3.4e +/-38)?

P.S. How did u put in a piece of code like you did?



Thanks
 
The float's range is between -3.4e38 ... +3.4e38

<code>

</code>
 
Thanks, tamjidka,

you are saying (3.4e +/-38) actually means

-3.4e38 ... +3.4e38?

I thought 3.4e +/-38 meant 3.4*e^(-38) ~ 3.4*e^(+38).

This is a weird expression, I gotta say.


Thanks
 
Those are HTML tags. This site uses TGML tags, so replace < & > with [ & ].

Click the "Process TGML" link above the Submit Post button to see all the tags you can use.
 
I think you are correct Wuu, 3.4e +/-38 means 3.4*e^(-38) ~ 3.4*e^(+38). I think it is just a typo on the page or they assume you know it is either positive or negative.
 
Beware: the C++ language standard does not define floating point types ranges and precisions.
But you may print these values for every standard C++ implementation with this portable program:
Code:
#include <iostream>
#include <limits>
using namespace std;

typedef numeric_limits<long double> LongDouble;
typedef numeric_limits<double> Double;
typedef numeric_limits<float> Float;

const char* range = " .. ";
const char* is = "]\t";

int main()
{
 cout.precision(Float::digits10);
 cout << "float [" << sizeof(float) << is
 << Float::min() << range << Float::max() << endl;
 cout.precision(Double::digits10);
 cout << "double [" << sizeof(double) << is
 << Double::min() << range << Double::max() << endl;
 cout.precision(LongDouble::digits10);
 cout << "long double [" << sizeof(long double) << is
 << LongDouble::min() << range << LongDouble::max() << endl;
 
 return 0;
}
 
Apropos: min value for floats usually means as min positive value (it is intended all knows that fact: floats are signed entities;)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top