Arm Mbed OS support forum

Calculating receiprocal results in zero

Why can’t I calculate the reciprocal? If I use freq^-1 for 600 I get -601, if I do 1/600 I get zero.

Is this another option I need to enable in the mbed_lib.json file like I had to enable for floats in mbed 6.0?

float time = 1/freq;
printf("Freq: %i, Time: %f\r\n", freq, time);

It appears that freq is an integer. The calculation then proceeds like this:
float time = (float) ( ( (int) 1 ) / ( (int) freq ) ).
So, all calculation is done as int, and the final result is then cast to a float.

You could probably just change the 1 to 1.0.

This does not work on mbed 6.0. If you roll back to ~5.12 it works fine though.

#include <iostream>

using namespace std;

int main() {

double number = 600;

double reciprocal = -1;

reciprocal = 1.0/number;

cout << reciprocal << "\r\n" << endl;

return 0;

}

minimal_printf is new default, please check this: