How to find the length of the mantissa on a particular machine?

I want to find the number of mantissa digits and the rounding of the unit on a particular computer. I understand what it is, I just don’t know how to find them, although I understand that they can vary from computer to computer.

I need this number to perform certain aspects of numerical analysis, for example, to analyze errors.

Currently, I think I could write a small C ++ program to slowly increase the number until overflow occurs, but I'm not sure which type of number to use.

Am I on the right track? How exactly is this done to calculate this?

+3
source share
3

, , , , float. , Java IEEE ( , 754).

, , , 0.5 1, , . , 0.25 1, 0.125 1 , , - :

float a = 1;
float b = 0.5;
int bits = 0;
while (a + b != a) {
    bits = bits + 1;
    b = b / 2;
}

3 , 1 + 1/16 1.

.

2, 1, IEEE754 "1+" .

EDIT:

, , , , 63 , 4 .

( , [ while (((float)(a + b) != (float)(a))] ) (, , ) , a b, , .

, , , IEEE754 ( ).

. , - , , , ( , : -).

2:

, , . , - .

#include <stdio.h>
#include <float.h>

int main(void) {
    float a = 1;
    float b = 0.5;
    float c = a + b;
    int bits = 1;
    while (c != a) {
        bits = bits + 1;
        b = b / 2;
        c = a + b;
    }
    printf("%d\n",FLT_MANT_DIG);
    printf("%d\n",bits);
    return 0;

}

(24,24), , .

C, ( , , ). C, Eclipse Ubuntu: -).

+5

C ++ <float.h> <cfloat>.

C99 5.2.4.2.2 :

  • FLT_RADIX
  • FLT_MANT_DIG
  • FLT_DIG
  • FLT_EPSILON
  • FLT_MIN_EXP
  • FLT_MIN
  • FLT_MIN_10_EXP
  • FLT_MAX_EXP
  • FLT_MAX
  • FLT_MAX_10_EXP

DBL LDBL ( DBL_RADIX LDBL_RADIX). , IEEE 754 ( IEEE 754, 1999 , 2008 ).

+1

You might want to check <limits>in your C ++ library:

#include <iostream>
#include <limits>
#include <typeinfo>

template <typename T>
void printDetailsFor() {
    std::cout
        << "Printing details for " << typeid(T).name() << ":\n"
        << "\tradix:        " << std::numeric_limits<T>::radix        << "\n"
        << "\tradix digits: " << std::numeric_limits<T>::digits       << "\n"
        << "\tepsilon:      " << std::numeric_limits<T>::epsilon()    << "\n"
        << std::endl;
}

int main() {
    printDetailsFor<int>();
    printDetailsFor<float>();
    printDetailsFor<double>();
    printDetailsFor<long double>();
    return 0;
}

I think what you want std::numeric_limits<T>::digits, which should be more than the number of bits of the mantissa. My car prints:

Printing details for i:
    radix:        2
    radix digits: 31
    epsilon:      0

Printing details for f:
    radix:        2
    radix digits: 24
    epsilon:      1.19209e-07

Printing details for d:
    radix:        2
    radix digits: 53
    epsilon:      2.22045e-16

Printing details for e:
    radix:        2
    radix digits: 64
    epsilon:      1.0842e-19
+1
source

Source: https://habr.com/ru/post/1702562/


All Articles