473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

numeric_limits<float> questions

The code:

#include <iostream>
#include <limits>
int main()
{
using namespace std;

typedef numeric_limits<floatlimitsf;

cout<< boolalpha<< " radix= "<< limitsf::radix<< endl
<<" digits= "<< limitsf::digits<< endl
<< " digits10= "<< limitsf::digits10<< endl
<< " min()= "<< limitsf::min()<< endl
<< " max()= "<< limitsf::max()<< endl
<< " epsilon()= "<< limitsf::epsilon()<< endl
<< " min_exponent= "<< limitsf::min_exponent<< endl
<< " min_exponent10= "<< limitsf::min_exponent10<< endl
<< " max_exponent= "<< limitsf::max_exponent<< endl
<< " max_exponent10= "<< limitsf::max_exponent10<< endl
<< " is_iec559= "<< limitsf::is_iec559<< endl
<< " traps= "<< limitsf::traps<< endl;

}
in my system produces:
[john@localhost src]$ ./foobar-cpp
radix= 2
digits= 24
digits10= 6
min()= 1.17549e-38
max()= 3.40282e+38
epsilon()= 1.19209e-07
min_exponent= -125
min_exponent10= -37
max_exponent= 128
max_exponent10= 38
is_iec559= true
traps= false

[john@localhost src]$

numeric_limits<floatquestions:

Q1: min() shows up as a positive number. Why?

Q2: min() contains e-38, while min_exponent10== -37.
Oct 31 '07 #1
4 4411
numeric_limits<floatquestions:
>
Q1: min() shows up as a positive number. Why?
Because min(), for floating point types, shows the
smallest positive value and not the minimum value
like integral types. Not sure why this is the case.
I would have preferred min() to work the same in all cases.
Maybe add a smallest() function to do what min() does now.

At any rate, what you probably want in -max().


Oct 31 '07 #2
john <jo**@no.spamwrites:
The code:

#include <iostream>
#include <limits>
int main()
{
using namespace std;

typedef numeric_limits<floatlimitsf;

cout<< boolalpha<< " radix= "<< limitsf::radix<< endl
<<" digits= "<< limitsf::digits<< endl
<< " digits10= "<< limitsf::digits10<< endl
<< " min()= "<< limitsf::min()<< endl
<< " max()= "<< limitsf::max()<< endl
<< " epsilon()= "<< limitsf::epsilon()<< endl
<< " min_exponent= "<< limitsf::min_exponent<< endl
<< " min_exponent10= "<< limitsf::min_exponent10<< endl
<< " max_exponent= "<< limitsf::max_exponent<< endl
<< " max_exponent10= "<< limitsf::max_exponent10<< endl
<< " is_iec559= "<< limitsf::is_iec559<< endl
<< " traps= "<< limitsf::traps<< endl;

}
in my system produces:
[john@localhost src]$ ./foobar-cpp
radix= 2
digits= 24
digits10= 6
min()= 1.17549e-38
max()= 3.40282e+38
epsilon()= 1.19209e-07
min_exponent= -125
min_exponent10= -37
max_exponent= 128
max_exponent10= 38
is_iec559= true
traps= false
That looks wrong to me. I think min_exponent should be -126. On my
system, this program

#include <iostream>
#include <limits>

template <typename float_t, typename sint_t, typename uint_t, int mbits, int ebits>
class ieee_float {
#ifdef __BIG_ENDIAN__
uint_t s:1;
uint_t e:ebits;
uint_t m:mbits;
#else
uint_t m:mbits;
uint_t e:ebits;
uint_t s:1;
#endif

public:
static const uint_t mdenom = ((uint_t)1 << mbits);
static const uint_t ebias = ((uint_t)1 << (ebits - 1)) - 1;
sint_t sign(void) const { return 1 - 2*s; }
sint_t exponent(void) const { return e ? e - ebias : -(ebias - 1); }
uint_t mantissa(void) const { return ((uint_t)(!!e) << mbits) | m; }
bool infinity(void) const { return (e == ((1 << ebits) - 1)) && (m == 0); }
bool nan(void) const { return (e == ((1 << ebits) - 1)) && (m != 0); }
bool denormal(void) const { return (e == 0) && (m != 0); }
ieee_float(float_t f) { *(float_t *)this = f; }
ieee_float(uint_t u) { *(uint_t *)this = u; }
operator float_t() const { return *(float_t *)this; }
operator uint_t() const { return *(uint_t *)this; }
};

typedef ieee_float<float, int, unsigned, 23, 8single_precision;
typedef ieee_float<double, long long, unsigned long long, 52, 11double_precision;

int main(int argc, char *argv[])
{
single_precision d(std::numeric_limits<float>::min());
std::cout << "decimal: " << (float) d << std::endl;
std::cout << "hexadecimal: " << std::hex << (unsigned) d << std::dec << std::endl;
std::cout << "sign: " << d.sign() << std::endl;
std::cout << "exponent: " << d.exponent() << std::endl;
std::cout << "mantissa: " << d.mantissa() << "/" << d.mdenom << std::endl;
std::cout << "min exponent: " << std::numeric_limits<float>::min_exponent << std::endl;
return 0;
}

produced this output:

$ ./test
decimal: 1.17549e-38
hexadecimal: 800000
sign: 1
exponent: -126
mantissa: 8388608/8388608
min exponent: -125

Strange that the exponent on "std::numeric_limits<float>::min()" would
be less than "std::numeric_limits<float>::min_exponent".

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
Nov 1 '07 #3
Chip Coldwell wrote:
>
That looks wrong to me. I think min_exponent should be -126. On my
system, this program

#include <iostream>
#include <limits>

template <typename float_t, typename sint_t, typename uint_t, int mbits, int ebits>
class ieee_float {
#ifdef __BIG_ENDIAN__
uint_t s:1;
uint_t e:ebits;
uint_t m:mbits;
#else
uint_t m:mbits;
uint_t e:ebits;
uint_t s:1;
#endif

public:
static const uint_t mdenom = ((uint_t)1 << mbits);
static const uint_t ebias = ((uint_t)1 << (ebits - 1)) - 1;
sint_t sign(void) const { return 1 - 2*s; }
sint_t exponent(void) const { return e ? e - ebias : -(ebias - 1); }
uint_t mantissa(void) const { return ((uint_t)(!!e) << mbits) | m; }
bool infinity(void) const { return (e == ((1 << ebits) - 1)) && (m == 0); }
bool nan(void) const { return (e == ((1 << ebits) - 1)) && (m != 0); }
bool denormal(void) const { return (e == 0) && (m != 0); }
ieee_float(float_t f) { *(float_t *)this = f; }
ieee_float(uint_t u) { *(uint_t *)this = u; }
operator float_t() const { return *(float_t *)this; }
operator uint_t() const { return *(uint_t *)this; }
};

typedef ieee_float<float, int, unsigned, 23, 8single_precision;
typedef ieee_float<double, long long, unsigned long long, 52, 11double_precision;

int main(int argc, char *argv[])
{
single_precision d(std::numeric_limits<float>::min());
std::cout << "decimal: " << (float) d << std::endl;
std::cout << "hexadecimal: " << std::hex << (unsigned) d << std::dec << std::endl;
std::cout << "sign: " << d.sign() << std::endl;
std::cout << "exponent: " << d.exponent() << std::endl;
std::cout << "mantissa: " << d.mantissa() << "/" << d.mdenom << std::endl;
std::cout << "min exponent: " << std::numeric_limits<float>::min_exponent << std::endl;
return 0;
}

produced this output:

$ ./test
decimal: 1.17549e-38
hexadecimal: 800000
sign: 1
exponent: -126
mantissa: 8388608/8388608
min exponent: -125

Strange that the exponent on "std::numeric_limits<float>::min()" would
be less than "std::numeric_limits<float>::min_exponent".

Chip

Your code produces in my system:
[john@localhost src]$ ./foobar-cpp
decimal: 1.17549e-38
hexadecimal: 800000
sign: 1
exponent: -126
mantissa: 8388608/8388608
min exponent: -125

[john@localhost src]$

It looks like we get the same values. My OS is CentOS 5.0, with
"gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)".

Nov 1 '07 #4
john <jo**@no.spamwrites:
Chip Coldwell wrote:
>>
That looks wrong to me. I think min_exponent should be -126. On my
system, this program

#include <iostream>
#include <limits>

template <typename float_t, typename sint_t, typename uint_t, int mbits, int ebits>
class ieee_float {
#ifdef __BIG_ENDIAN__
uint_t s:1;
uint_t e:ebits;
uint_t m:mbits;
#else
uint_t m:mbits;
uint_t e:ebits;
uint_t s:1;
#endif

public:
static const uint_t mdenom = ((uint_t)1 << mbits);
static const uint_t ebias = ((uint_t)1 << (ebits - 1)) - 1;
sint_t sign(void) const { return 1 - 2*s; }
sint_t exponent(void) const { return e ? e - ebias : -(ebias - 1); }
uint_t mantissa(void) const { return ((uint_t)(!!e) << mbits) | m; }
bool infinity(void) const { return (e == ((1 << ebits) - 1)) && (m == 0); }
bool nan(void) const { return (e == ((1 << ebits) - 1)) && (m != 0); }
bool denormal(void) const { return (e == 0) && (m != 0); }
ieee_float(float_t f) { *(float_t *)this = f; }
ieee_float(uint_t u) { *(uint_t *)this = u; }
operator float_t() const { return *(float_t *)this; }
operator uint_t() const { return *(uint_t *)this; }
};

typedef ieee_float<float, int, unsigned, 23, 8single_precision;
typedef ieee_float<double, long long, unsigned long long, 52, 11double_precision;

int main(int argc, char *argv[])
{
single_precision d(std::numeric_limits<float>::min());
std::cout << "decimal: " << (float) d << std::endl;
std::cout << "hexadecimal: " << std::hex << (unsigned) d << std::dec << std::endl;
std::cout << "sign: " << d.sign() << std::endl;
std::cout << "exponent: " << d.exponent() << std::endl;
std::cout << "mantissa: " << d.mantissa() << "/" << d.mdenom << std::endl;
std::cout << "min exponent: " << std::numeric_limits<float>::min_exponent << std::endl;
return 0;
}

produced this output:

$ ./test
decimal: 1.17549e-38
hexadecimal: 800000
sign: 1
exponent: -126
mantissa: 8388608/8388608
min exponent: -125

Strange that the exponent on "std::numeric_limits<float>::min()" would
be less than "std::numeric_limits<float>::min_exponent".

Chip


Your code produces in my system:
[john@localhost src]$ ./foobar-cpp
decimal: 1.17549e-38
hexadecimal: 800000
sign: 1
exponent: -126
mantissa: 8388608/8388608
min exponent: -125

[john@localhost src]$

It looks like we get the same values. My OS is CentOS 5.0, with
"gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)".
Well, I asked the oracle, and he quoted me chapter and verse:

Similarly ISO C++98, [lib.numeric.limits.members]/{23,24,26,27}:
static const int min_exponent;
Minimum negative integer such that radix raised to the power of one less
than that integer is a normalized floating point number.189)
Meaningful for all floating point types.
static const int max_exponent;
Maximum positive integer such that radix raised to the power one less
than that integer is a representable finite floating point number.191)
Meaningful for all floating point types.

189) Equivalent to FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP.
191) Equivalent to FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP.

So "min_exponent" is the minimum exponent plus one (of course!).

What I haven't been able to discover is what motivated this intuition
defying choice.

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
Nov 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
4
by: Simon | last post by:
Hi, I am trying to create a round kind of function, (to round float and double to int). In the H file I can do the following with not too much trouble... // #include <limits> #include...
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
1
by: mathieu | last post by:
Hi there, I am dealing with the following problem: I need to convert a std::vector<T(where T can be any interget type: char, short, ushort) by applying a linear transform (a,b), following; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.