-3
#include <stdio.h>
#define s scanf
#define p printf
void main (){
    int P,I,A;
    float T,R;
    clrscr();
    p("PRINCIPAL: ");
    s("%i",&P);
    p("RATE: ");
    s("%f",&R);
    p("TERM: ");
    s("%f",&T);
    R = R/100;
    I = P*R*T;
    A = P+I;
    p("I: %i\nMA: %i",I,A);
    getch();
}

This really bugs me, if i put PRINCIPAL: 1200 RATE: 3 TERM:1 I: 35 MA: 1235 but if you compute in manually the answer should be I: 36 MA: 1236 the number is decreasing by 1. Why is it happening? why does the answer differ from computer and manual computing?

Amben Uchiha
  • 431
  • 2
  • 10
  • [This](https://ideone.com/dosphN) doesn't say so. – Ajay Brahmakshatriya Sep 19 '17 at 18:35
  • @EOF Although similar, this doesn't seem to be exactly the same question as that one. – Charles Srstka Sep 19 '17 at 18:40
  • 1
    @CharlesSrstka It's the canonical question about unexpected (for beginners) floating-point behavior. Fundamentally the same problem is displayed in this question. – EOF Sep 19 '17 at 18:42
  • @EOF While that's true, it seems to me that the *reason* that it is fundamentally the same problem would not be obvious to a beginner, since they appear to be asking two different things to someone unfamiliar with the behaviors of floating-point math. – Charles Srstka Sep 19 '17 at 18:45
  • @CharlesSrstka Well, stack overflow *does* have rules requiring the one asking the question to show *some* willingness to do their own research. Pointing out that floating-point problems are at the heart of the problem, and giving them a post discussing typical floating point problems should suffice. – EOF Sep 19 '17 at 18:49
  • @EOF Isn't asking questions like this how one learns, though? We all were new to this stuff at some point, and this *is* a confusing topic for beginners. – Charles Srstka Sep 19 '17 at 18:50
  • @CharlesSrstka No, asking bad questions does absolutely nothing to further the questioner's understanding. Now, formulating a question carefully might, and reading answers might, but not asking the question. – EOF Sep 19 '17 at 18:52
  • @EOF But in order to *know* that this problem was caused by floating-point behavior, OP would have already had to *understand* floating-point behavior. Otherwise, there's no way for the OP to know what to research. I think the question has merit, and should be preserved. – Charles Srstka Sep 19 '17 at 18:55
  • @CharlesSrstka And by marking the question as a duplicate of a question discussing floating-point behavior, the OP gets to understand that floating-point behavior is the cause of the behavior they observe. I fail to see the problem you have with this. I also fail to see what value this question, formatted as it is, will have for future reference for those who actually research themselves before asking a new question. – EOF Sep 19 '17 at 18:58
  • @EOF Well, for one thing, the linked answer does not discuss the behavior involved in casting floating-point variables to integers, which is at the root of the problem OP is seeing. – Charles Srstka Sep 19 '17 at 19:00
  • Fundamentally, the problem is that computer floating point arithmetic does not match people's mental model, not least because computers work with binary (unless you happen to have a Power6 or later CPU; those do have decimal floating point arithmetic) whereas people work in decimal. Assumptions that work in decimal (such as `0.03` has an accurate representation) are not valid in binary. You could help yourself by using `double` instead of `float`; by computing in `double`; by printing the values computed in `double`. That would improve the accuracy of the answers. It wouldn't stop truncation. – Jonathan Leffler Sep 19 '17 at 21:18

3 Answers3

1

You try to typecast float to int that causes some data loss. Just like we can not store the big object in the small bag.

#include <stdio.h>
#define s scanf
#define p printf
int main (){
    int P;
    float T,R,I,A;
    p("PRINCIPAL: ");
    s("%i",&P);
    p("RATE: ");
    s("%f",&R);
    p("TERM: ");
    s("%f",&T);
    R = R/100;
    I = P*R*T;
    A = P+I;
    p("\nI: %f\nMA: %f",I,A);
    return 0;
}
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
0

Your problem is in the conversion of float to int. If you do your program with everything typed as float, you get the expected results:

#include <stdio.h>
#define s scanf
#define p printf
int main (){
    float P,I,A;
    float T,R;

    p("PRINCIPAL: ");
    s("%f",&P);
    p("RATE: ");
    s("%f",&R);
    p("TERM: ");
    s("%f",&T);
    R = R/100;
    I = P*R*T;
    A = P+I;
    p("I: %f\nMA: %f",I,A);

    return 0;
}

outputs:

PRINCIPAL: 1200
RATE: 3
TERM: 1
I: 36.000000
MA: 1236.000000

However, when you convert your float values to int, you just take the integer part; everything to the right of the decimal point gets deleted. So, even though it's printing as 36.000000 when I do it, it's possible that the value of I may be coming out to something like 35.9999999, due to imprecision in floating-point math, and simply displaying as 36.000000 due to rounding in the display process. In this case, you'll just get the 35, and lose everything else.

To solve your problem, either leave everything as a float, or convert your floats to ints by rounding them—for example, by using lroundf in math.h—instead of just casting them.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
0

Your problem is with typecasting, please research on this subject a look at this, it's a little bit hude to explain in a simple text here. But I can tell you, you are problably losing information when casting from float to int, because this two primitive types in C have diferent max length. You can see changin the int variables to float and running your own program again, like this:

#include <stdio.h>
#define s scanf
#define p printf
void main (){
    float P,I,A;
    float T,R;
    p("PRINCIPAL: ");
    s("%f",&P);
    p("RATE: ");
    s("%f",&R);
    p("TERM: ");
    s("%f",&T);
    R = R/100;
    I = P*R*T;
    A = P+I;
    p("I: %f\nMA: %f",I,A);
    getch();
}

This will produce your desired output, just in float.