This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F28335: Fraction '1/2' leads to '0' when assigned to a float variable

Part Number: TMS320F28335

Tool/software: TI C/C++ Compiler

Hello all,

I am trying to assign a fraction '1/2' to a float variable and the result is '0' rather than '0.5' when I watch the variable in watch expressions.

Also, when I add a variable with '1/2' if won't reflect and when I add '0.5' it reflects in the variable.

Please help me out with this ambiguity. 

I have also tried changing the libraries but still the problem exists.

Thank You.

  • Hello,

    I believe that's actually the expected behavior. 1 and 2 are integers so the compiler will do integer division (1 / 2 = 0) before converting the result to a floating point value. If you were to write 1.0 / 2.0 or (float)1 / (float)2, it will do float division and give you 0.5.

    Whitney