0

Here is an extract from my code:

#include <iostream>
#include <nlohmann/json.hpp>
#include <vector>

using nlohmann::json;

void fillJs(json js, std::vector<float>* positions)
{
positions->push_back(js[0]);
positions->push_back(js[1]);
}


float precision(float f, int places)
{
float n = std::pow(10.0f, places);
return std::round(f * n) / n;
}

int main()
{ 
json js{45652.012222222, 1563.4500001, 5.71235, 20218.9194556};
std::cout << "js [0] = " << js[0] << std::endl;
float yy = js[3];
std::cout << "yy = " << yy << std::endl;
std::cout << "js 2 round 1 = " << precision(js[3], 1) << std::endl;
std::cout << "js 2 round 2 = " << precision(js[3], 2) << std::endl;
std::cout << "js 2 round 3 = " << precision(js[3], 3) << std::endl;

std::vector<float> vec;
vec.push_back(js[0]);
std::cout << "vec 0 = " << vec[0] << std::endl;
std::vector<float> vec1;
fillJs(js, &vec1);
std::cout << "vec 1 = " << vec1[1] << std::endl;
}

And here is the output:

js [0] = 45652.012222222
yy = 20218.9
js 2 round 1 = 20218.9
js 2 round 2 = 20218.9
js 2 round 3 = 20218.9
vec 0 = 45652
vec 1 = 1563.45

Why is the float number rounded when assigned ant not rounded wen printed ?

I need to assign the values without rounding them, any idea please ?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
SADL
  • 37
  • 5
  • 1
    `float` is only precise to about 7 digits, that's 7 total digits, not digits after the decimal point. Printing to more than this precision is basically printing noise. See also: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) - not quite a duplicate but has the necessary background. – Richard Critten May 22 '23 at 13:04
  • what output did you expect? What do you mean with "not rounded wen printed ?" ? – 463035818_is_not_an_ai May 22 '23 at 13:05
  • 2
    What is type of `js[0]`? What `operator<<` does for that type? – Marek R May 22 '23 at 13:06
  • And of course what happens with type in `float yy = js[3];`? – Marek R May 22 '23 at 13:18
  • There's no indication that [the `<<` operator](https://json.nlohmann.me/api/operator_ltlt/) passes any info about the precision on to the [`dump`](https://json.nlohmann.me/api/basic_json/dump/) function. `dump` doesn't provide any parameters for doing so anyways and it returns a string object, i.e. nothing that could take the precision of the stream it's written to into account... – fabian May 22 '23 at 13:25
  • 2
    The default floating-point type in `nlohmann::json` is `double`, not `float`. The default precision for printing floating-point numbers is six significant digits. – molbdnilo May 22 '23 at 13:36
  • @463035818_is_not_a_number i look for values not rounded when assigned and pushed in the vector – SADL May 22 '23 at 13:51
  • 1
    @SADL You should change your vector to `vector` – john May 22 '23 at 14:00
  • Note that none of those numbers have an exact representation as either `float` or `double`. See [here](https://coliru.stacked-crooked.com/a/2a6b4fab2488ee12). – molbdnilo May 22 '23 at 15:51

0 Answers0