0

I can't disable scientific notation... I'd like to print on screen the exact value of the numbers instead of the scientific notation. It's my first project in C. It's a simple calculator it's just a prototype, I'd like to create a very complex program starting from this. Any help or tips will be really appreciated. Thanks for your support.

#include <math.h>
#include <stdio.h>
#define ADD(a, b) (a += b)
#define SOT(a, b) (a -= b)
#define PER(a, b) (a *= b)
#define DIV(a, b) (a /= b)
#define POT(a, b) a = pow(a, b)
#define LOG(a, b) a = (log10(a) / log10(b))
#define PERC(a) a = (a / 100)

{
    float a, b, d;
    static float e;
    char c, g;
    printf("Calcola \n");
    scanf("%g", &a);
    while (1) {
        e = a;
        scanf(" %c", &c);
        switch (c) {
        case '+':
            scanf("%g", &b);
            system("clear");
            d = ADD(a, b);
            printf(" \n Risultato \n \n");
            printf("(%g+%g)=", e, b);
            printf("%g", d);
            break;
        case '-':
            scanf("%g", &b);
            system("clear");
            d = SOT(a, b);
            printf(" \n Risultato \n \n");
            printf("(%g-%g)=", e, b);
            printf("%g", d);
            break;
        case '*':
            scanf("%g", &b);
            system("clear");
            d = PER(a, b);
            printf(" \n Risultato \n \n");
            printf("(%g*%g)=", e, b);
            printf("%g ", d);
            break;
        case '/':
            scanf("%g", &b);
            system("clear");
            d = DIV(a, b);
            printf(" \n Risultato \n \n");
            printf("(%g/%g)=", e, b);
            printf("%g", d);
            break;
        case '^':
            scanf("%g", &b);
            system("clear");
            d = POT(a, b);
            printf(" \n Risultato \n \n");
            printf("(%g^%g)=", e, b);
            printf("%g", d);
            break;
        case 'l':
            scanf("%g", &b);
            system("clear");
            d = LOG(a, b);
            printf(" \n Risultato \n  \n");
            printf("(%glog%g)=", e, b);
            printf("%g", d);
            break;
        case '%':
            system("clear");
            d = PERC(a);
            printf(" \n Risultato \n \n");
            printf("%g%=", e);
            printf("%g", d);
            break;
        }
    }
    return 0;
}
Acorn
  • 24,970
  • 5
  • 40
  • 69
  • How do you want to print the exact value of 1/3? – n. m. could be an AI Aug 18 '19 at 13:08
  • "I'd like to print on screen the exact value of the numbers instead of the scientific notation." --> To print the _exact_ value of a `double`, [sample code](https://codereview.stackexchange.com/questions/212490/function-to-print-a-double-exactly). Simone Garzieri, do you truly want the _exact_ decimal value or can a rounded value do? – chux - Reinstate Monica Aug 18 '19 at 14:53
  • 1/3=0,333333333 and 1/2=0.5 I want that the program dynamically sets the needed amount of decimal numbers to use. – Simone Garzieri Aug 18 '19 at 15:26
  • I found a workaround %.12Lg. The program automatically sets the decimals, with 12 value precision. – Simone Garzieri Aug 19 '19 at 18:19

1 Answers1

0

Use the f format specifier, not g:

converts floating-point number to the decimal notation in the style [-]ddd.ddd.

Example:

printf ("%f", d);

instead of:

printf ("%g", d);
Acorn
  • 24,970
  • 5
  • 40
  • 69
  • I use g because the program dynamically sets the decimal numbers, if I use f I have to specify a fixed amount of decimals. Is there a way to use g without scientific notation? – Simone Garzieri Aug 18 '19 at 14:08
  • @SimoneGarzieri I am not sure I understand, your example code does not show that. Anyway, you can always build the format string and pass the required precision you want each time using `f`! – Acorn Aug 18 '19 at 14:58
  • 1
    @SimoneGarzieri If I understand what you're asking, there isn't a way to do it -- because it turns out what you're asking to do doesn't make sense. If you want to print 1.23 as `1.23` (not `1.230000`), and 1.234 as `1.234` (not `1.234000`), this is difficult to impossible to do, because in binary floating point, *there is no such number* as 1.23, or 1.234. You need to decide how many digits of precision you want, and print that many (rounding if necessary), not try to print the numbers as what they "really are", because that's a remarkably slippery concept. – Steve Summit Aug 18 '19 at 15:03
  • @SimoneGarzieri Indeed, he would need to integer math for that. – Acorn Aug 18 '19 at 15:10
  • Acorn thanks for your support and kindness. My goal is to recreate the exact same behaviour of a traditional (non scientific) Calculator. The calculator automatically identifies the characteristic of a number (if it's a simple int it doesn't show any decimal number and if it's a float number shows the needed amount of decimal number). Using the %g format in printf allow me to do that but the problem is that if I use a value too high or too many decimals the scientific notation will automatically appear. I'd like to use the %g format without triggering the scientific notation, is that possible? – Simone Garzieri Aug 18 '19 at 15:12
  • Steve Summit thanks for your reply. What about option scipen=999 Can I use it? and if yes how can I use it? – Simone Garzieri Aug 18 '19 at 15:14