This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

formatted output("back to c basic")

hi there
please here is the sample prog. code

#include stdio.h
#include conio.h

void main()
     {
     clrscr();
     float x=98.2344;
     printf("%2.2f",x);
     getch();
     }


so output of this prog.
is

98.23

upto here all thing's are ok
but it is possible to move or assing the float value
with limited number after decimal point;

for example in above prog if we are taking the new float variable new_x and moving the x to new_x i.e.
if x =98.2344
and i want to assign new_x = 98.23 or 98.2 or 98.234

  • "it is possible to move or assing the float value
    with limited number after decimal point;"

    Round x at whatever precision you want when assigning to new_x.

    www.diycalculator.com/sp-round.shtml

  • But how to do it ?
    can you explain me with the example in c please

  • This is not really 'C basics' but 'floating point basics'.

    There are a few facts that you must have in mind:

    A) 'C' floating point numbers are generally based on the IEEE754 binary floating point representation. In this format the numbers are stored as binary approximations of the decimal number you pretent it to be.

    B) The binary precision and resolution depend on how many bits it has for the mantissa. Ordinary float types have only 23bits of mantissa.

    C) Quite many decimal fractional numbers have no exact binary representation, regardless of the mantissa width. When represented in floating point, these numbers lose precision, and are rounded.

    D) Arithmetic operations in floating point numbers inevitably cause rounding and precision loss.

    E) This is why you should never test a floating point value for equality. Never use '==' in a compare for floating point numbers, but only '<', '>', '<=', '>='.

    The compiler Clib normally has a number of floating point functions for rounding and truncation, but you still should not test for hard equality, even when using rounded numbers.

    The Keil C for the 8051 circumvents this issue by allowing you to set a range of precision when performing float compare ops. You can select the number of least significant bits to ignore in the compare, to allow for slightly different values to compare equal.

    In any case, it is much safer and portable to rewrite the algorithms as not to use hard equality.

  • all this thing's are ok
    but if you explain this with example in c that will much more precious

  • Without knowing your floating point library, I can't give you a C code that will work for your system.

    Some libs have rounding and truncation functions, like mfp_trunc() or fp_round(), that allow you to reduce the precision of the fp number to some decimal place, but the majority of uC fplibs don't. HOWEVER, these functions may not guarantee exact decimal chopping of the number, because of the inherent binary approximation.

    An 'adequate' approach that is independent from fp library rounding functions could be:

    ///////////////////////////////////////////////////
    // return a float truncated to a specified decimal
    // precision.
    //
    // ENTRY:
    //          number      float number to truncate
    //          precision   desired decimal precision, in
    //                      a power of 10, i.e., for 2 decimal
    //                      places use precision = 100, for 3
    //                      decimals use 1000.
    // RETURNS:
    //          float number truncated
    ///////////////////////////////////////////////////
    float fp_round(float number, unsigned int precision)
    {
        return((float) (((long) (number * (float) precision))/(float) precision));
    }
    
    int main(void)
    {
        float f1, f2, f3;
    
        f1 = 3.141592653589793;         // makes f1=3.141593
    
        // the do{}while is to avoid compiler optimize-out
        do
        {
            f2 = fp_round(f1, 1000);    // f2 = 3.141
            f3 = fp_round(f1, 10);      // f3 = 3.1
        } while (f2 < f3);
    }
    
    

    The above code compiles in about any compiler. It uses integer type castings, so it does not requires any special fplib code. This is just exampleware to illustrate my point, you can certainly optimize the concept.

    You can, of course, use double instead of foat, and you can also implement it as a macro or inline function.

    This approach may be slower than specific round() or trunc() functions, but it is lib-independent, and probably smaller than lib-specific functions.