help needed in a c program

rohitvishnu

rohitvishnu

@rohitvishnu-aLqdqe Oct 26, 2024

here is a given c program which rounds off the floating value.



This job is to have the return amount in both calls to return 1.32.

Now the 1.324 returns 1.32000001..this is not acceptable, this must return the correct rounding values.




//***************************************************************//
//***************************************************************//
void TestTax()
{
float Tax_Amt1 = 1.324;
float Tax_Amt2  =1.325;
float Ret_Amt;

    Ret_Amt = Round_Money(Tax_Amt1);
    Ret_Amt = Round_Money(Tax_Amt2);
}
//***************************************************************//
//***************************************************************//
float Round_Money(float In_Amt)
{
char temp1[32];
char temp2[32];

float Ret_Amt;

int t1,t2;
float r1,r2;
int v1,v2;

int t=0;

    sprintf(temp1,"%.3f",In_Amt);
    In_Amt = atof(temp1);

    t1 = abs(In_Amt);
    r1  = In_Amt - t1;

    sprintf(temp1,"%.03f",r1);
    
    temp2[0] = temp1[4];
    temp2[1] = 0;
    v1 = atoi(temp2);

    r2 = atof(temp2);

    sprintf(temp1,"%.2f",In_Amt);
    temp1[4] = '0';
    In_Amt = atof(temp1);


    if(v1 > 4)
    {
        In_Amt = In_Amt +.01;
        sprintf(temp1,"%.2f",In_Amt);
        Ret_Amt = atof(temp1);
    }
    else
    {
        sprintf(temp1,"%.2f",In_Amt);
        Ret_Amt = atof(temp1);

    }

    return Ret_Amt;
}


i am not able find the problem here..can you please help me out. it needs to round off 1.324 and 1.325 to 1.32

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Jun 23, 2009

    rohitvishnu, in the given code you are converting the value to a required precision using format specifiers. But again you are storing the result in a float variable.

    Due to the way a float or double value gets stored in memory, its not possible to store all numbers with correct precision in memory. There may be loss of some precision.

    In your case when you store 1.32 into memory, there is some loss of precision and hence you are not getting the exact value.

    Try the below code:

    #include "stdio.h"
    
    int main() {
      float f = 1.32;
      printf("Value Stored in memory: %64.63e\n", f);
      return 0;
    }
    

    The output of the code is:

    Value Stored in memory: 1.320000052452087402343750000000000000000000000000000000
    000000000e+00

    Though we have assigned value 1.32 to a float variable, the output is not exactly 1.32 (output specify the value stored in the memory with loss of some precision).

    But you can use workarounds to avoid such issues, e.g.,
    1. If you want to output the value, use format specifiers as you used in your code.
    2. If you want to compare value then make sure that the two values to be compared are of same type (e.g., compare float with float and double with double).

    PS: For more details on representation of variable in memory and loss of precision in floats and doubles refer post:
    #-Link-Snipped-#

    Let mew know if any item need more clarification.

    -Pradeep

  • ashutoshglamour

    ashutoshglamour

    @ashutoshglamour-YcpJXd Jul 2, 2009

    what is SPRINTF?

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Jul 2, 2009

    Hi ashutoshglamour, instead of posting new query in any existing thread please start a new thread for new query.

    -Pradeep

  • ashutoshglamour

    ashutoshglamour

    @ashutoshglamour-YcpJXd Jul 2, 2009

    pradeep_agrawalHi ashutoshglamour, instead of posting new query in any existing thread please start a new thread for new query.

    -Pradeep

    sorry but this question is related one.😁

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Jul 2, 2009

    I just forgot that the function sprintf is being used in the code given in the first post of the thread. My apologies.

    If you would have mentioned the context while posting the query that might have helped 😀

    Anyway, coming back to original query.

    sprintf is a C function similar to printf, i.e., it formats the value and output the value.

    The difference wrt printf is that printf function output the result of formatting to output string, whereas sprintf output the result in the provided string buffer, e.g.,

    The given code has a sprintf statement as:
    sprintf(temp1,"%.3f",In_Amt);
    where them1 is a character array of size 32 (string buffer). When the statement is executed, it represent the value of variable In_Amt as per the given format specifier "%.3f" and store the formatted string in temp1.

    Let me know if any point need more clarification.

    -Pradeep

  • ashutoshglamour

    ashutoshglamour

    @ashutoshglamour-YcpJXd Jul 3, 2009

    pradeep_agrawalI just forgot that the function sprintf is being used in the code given in the first post of the thread. My apologies.

    If you would have mentioned the context while posting the query that might have helped 😀

    Anyway, coming back to original query.

    sprintf is a C function similar to printf, i.e., it formats the value and output the value.

    The difference wrt printf is that printf function output the result of formatting to output string, whereas sprintf output the result in the provided string buffer, e.g.,.



    The given code has a sprintf statement as:
    sprintf(temp1,"%.3f",In_Amt);
    where them1 is a character array of size 32 (string buffer). When the statement is executed, it represent the value of variable In_Amt as per the given format specifier "%.3f" and store the formatted string in temp1.

    Let me know if any point need more clarification.

    -Pradeep

    thank you pradeep for clearfing my doubt.