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

Inacurate precision truncating.

Hi folk,
I have

u16 wTemp = 0;
fsingle fTemp = 17.5123;

wTemp = (u16)fTemp;

Somehow, the wTemp is 17. Technically, the wTemp should be 18, do anybody know what do I have to do for the compiler in order to generate more precise data conversion???

Thanks!

Parents
  • I've always performed my rounding explicitly. For your example I would code it thusly:

    u16 wTemp = 0;
    fsingle fTemp = 17.5123;
    
    wTemp = (u16)(fTemp + 0.5);
    
    Adding 0.5 will round a float to the nearest integer for any float value, not just the value in your example.

    --Dan Henry

Reply
  • I've always performed my rounding explicitly. For your example I would code it thusly:

    u16 wTemp = 0;
    fsingle fTemp = 17.5123;
    
    wTemp = (u16)(fTemp + 0.5);
    
    Adding 0.5 will round a float to the nearest integer for any float value, not just the value in your example.

    --Dan Henry

Children
No data