How to display 23 bytes long double answer in C?

This is a discussion on How to display 23 bytes long double answer in C? within the C Programming forums, part of the General Programming Boards category; How to display 23 bytes long double answer in C? long double can only show 16 bytes in windows......? "15648934568488.45684 ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    How to display 23 bytes long double answer in C?

    How to display 23 bytes long double answer in C?
    long double can only show 16 bytes in windows......?
    "15648934568488.45684 * 1561 +1265465.56
    =24427986862675946.68724"
    how to show?
    i can only show
    24427986862675944.00000

    source code:
    -----------------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    #define MAXOP 100
    #define NUMBER '0'

    #define MAXVAL 100

    #define BUFSIZE 100

    int getop(char []);
    void push(long double);
    long double pop(void);

    int sp = 0;
    long double val[MAXVAL];


    char buf[BUFSIZE];
    int bufp = 0;

    int getch(void);
    void ingetch(int);

    main() {
    int type;
    long double op2;
    char s[MAXOP];

    while ((type = getop(s)) != EOF) {
    switch (type) {
    case NUMBER:
    push(atof(s));
    break;
    case '+':
    push(pop() + pop());
    break;
    case '*':
    push(pop() * pop());
    break;
    case '-':
    op2 = pop();
    push(pop() - op2);
    break;
    case '/':
    op2 = pop();
    if (op2 != 0.0)
    push(pop() / op2);
    else
    printf("error: zero divisor\n");
    break;
    case '\n':
    printf("%.5lf\n", pop());
    break;
    default:
    printf("error: unknown command %s\n", s);
    break;
    }
    }
    return 0;
    }

    void push(long double f) {
    if (sp < MAXVAL)
    val[sp++] = f;
    else
    printf("error: stack full, can't push %g\n", f);
    }

    long double pop(void) {
    if (sp > 0)
    return val[--sp];
    else {
    printf("error: stack empty\n");
    return 0.0;
    }
    }

    int getch(void) {
    return (bufp >0) ? buf [--bufp] : getchar();
    }

    void ungetch(int c) {
    if (bufp >= BUFSIZE)
    printf("ungetch: too many characters\n");
    else
    buf[bufp++] = c;
    }

    int getop(char s[]) {
    int i, c;

    while ((s[0] = c = getch()) == ' ' || c == '\t')
    ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.')
    return c;
    i = 0;
    if (isdigit(c))
    while (isdigit(s[++i] = c = getch()))
    ;
    if (c == '.')
    while (isdigit(s[++i] = c = getch()))
    ;
    s[i] = '\0';
    if (c != EOF)
    ungetch(c);
    return NUMBER;
    }
    -------------------------------------------------
    input as follow:
    15648934568488.45684 1561 * 1265465.56 + <enter>

    Thanks a lot~~~

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Does this help?
    Code:
    int main ( ) {
        long double foo = 12345.67890;
        printf( "%Lf\n", foo );
        printf( "%.0Lf\n", foo );
        return 0;
    }
    > How to display 23 bytes long double answer in C?
    Technically, you can't. In my implementation at least, long double's have only 18 decimal digits of accuracy, so the last 5 digits of anything you write will be in effect noise.

  3. #3
    Sayeh
    Guest
    Why not typedef your own 'long triple'? That's how the compiler maker would do it. That is what 'typedef' is for-- creating new types...

    Neither the computer nor the compiler care one wit about the size of the number-- the control is on _your_ hands...

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    But typedef cannot create a type to show all the digits......maximum is still long double...
    Am I right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  5. converting double to long int
    By gunshipolitico in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2005, 09:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21