AnsweredAssumed Answered

Trouble printing float number normally

Question asked by Sage on Apr 3, 2015
Latest reply on Apr 4, 2015 by Sage
Attached is my modification of tiny_printf.c where I simply added float printing function (case 'f'). By default, the decimal points is 6.

Below are floats I tried to print:

Float                                                      Output
0.01                                                          0.10000
0.1                                                            0.100000
2.001                                                        2.1000
2.127                                                        2.127000
4.669                                                        4.669000

Highlighted part of my tiny_printf.c :

unsigned short precision = 6;

static const unsigned long dv[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };

static const float p[] = {0.0,10.0,100.0,1000.0,10000.0,100000.0,1000000.0,10000000.0,100000000.0,1000000000.0 };

case '.':
                  precision = *(++fmt) - '0';
                  if(!(precision >= 0 && precision <= 6))
                      precision = 6;
                  ++fmt;

 case 'f':
                {
                    float fval;
                    fval = va_arg(va, double);
                    if(fval < 0.0) {
                        fval *= -1.0;
                        *buf++ = '-';
                    }
                    long heiltal = (long)fval;
                    ts_itoa(&buf, heiltal, 10);
                    *buf++ = '.';
                    long frac = (long)(fval * p[precision]);
                    long decimal = frac - heiltal * dv[precision];
//                    long decimal = (long)((fval - heiltal) * dv[precision]);
                    ts_itoa(&buf, decimal, 10);
                    precision = 6;
                 }
                 break;
What's the problem with my coding !?

And as I increased the float by timer handler or while loop, for instances,
float ff = 1.28;
ff = ff + 0.01;

the result started getting crazy.. OUTPUT:
1.280000
1.290000
1.300000
1.310000
1.320000
1.330000
1.340000
1.340009
1.350008
1.360007
.
.
.
1.679999
1.690001
1.700002
.
.

The increment wasn't as expected.. Though the difference is small. I suspect it's because the floating point unit is not at its peak because I tried print only the decimal places by multiplying them with 1000, and the result remained the same:

Perhaps you might suggest me a better way of writing it~ Thousand thanks in advance! ^_^

Attachments

Outcomes