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

Using float variable

Hello everyone !

I have a problem with the float variables.

For example, i define float a = 123456123456.1234 and char lcd[30];

I used sprintf function to convert a to string and store into lcd (sprintf(lcd,"%0.2f",a);).

Then, i export lcd to LCD 16x2.

Result i see on the LCD is "123456100000.00"

Why this ?

And can everyone show me how to print a on LCD ("123456123456.1234")?

Best regard !

  • And can everyone show me ...

    Someone likely could, but everyone? My wife knows nothing about it and cares even less.

  • In the LCD result there are a lot "0" before the dot. May be it´s a multiply failure in how the float is convertet into the chars. Plus the number "123456123456.1234" don´t fit into 16 character, with the dot it consists of 17 characters.

  • Please take a closer look at the float data type.

    If you have a signed 4-byte integer, the largest number you can store is about 2.000.000.000, so about 9 value digits.

    If you instead use the 4 bytes for a floating point value, where some of the room is needed for mantissa and some for exponent, then you aren't likely to be able to store even more value digits than if you fit an integer in that same storage space...

    If you want to play with lots of value digits, then you need to play with some form of fixed-point arithmetic - and implement or use a big-number library where the value is stored in an array of bytes or integers.

  • Thank for reply !

    i'm tried with the double variable, but not success.

    do you have any solutions ?

  • "do you have any solutions ?"

    As I already suggested:
    "If you want to play with lots of value digits, then you need to play with some form of fixed-point arithmetic - and implement or use a big-number library where the value is stored in an array of bytes or integers."

    You did check the manual, to figure out the size for "float" and "double" data types when using the C51 compiler?

  • a lot of people 'automatically' go for float when it is not neded. Especially in small (e.g.'51) processors that is a BIG mistake. For a '51 to process a float it need a LOT of cycles.

    as an example: if all your values have up to two digits after the decimal point process everything as 100 * actual value and just insert the decinmal point in the appropiate place on the display

    Erik