How to get more precision in floating-point numbers in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <math.h>

// Keep in mind that a double cannot exactly represent most decimal fractions. 
// If you need to preserve every digit exactly (for example, 123.456789123456789), 
// store the original value as a string and extract digits from the string rather 
// than from a floating-point type.

int main(void)
{
    long double number = 123.456789123456789L;
    char buffer[64] = "";
    
    snprintf(buffer,
             sizeof(buffer),
             "%.15Lf",
             fabsl(number));


    printf("%s\n", buffer);

    return 0;
}


// 123.456789123456789L;
// 123.456789123456789


/*
run:

123.456789123456789

*/

 



answered Oct 15 by avibootz
...