How to avoid floating point problems?
-
@JonB
I am fine with storing the number as a string. But how can I tell QJsonDocument to use my string as a number, without converting it to a double first? Should I use a custom json parser instead?@Axel-Spoerl
For sake of avoiding confusion I'm only including the related parts of the code and renaming variable names to their type.// Some where in the dialogs constructor ui->lineEdit->setValidator(new QDoubleValidator()); // Some where in a slot float value = m_ui->lineEdit->text().toFloat(); // Some where in the saving json process: QJsonObject json; json["r"] = value; // ... QJsonDocument document(json); QByteArray data = document.toJson();
These are currently the only places the value gets read and used.
When the value of the lineEdit is 13.23, the json in the byte array says:
{ "r": 13.229999542236328, // ... }
@mhn2 said in How to avoid floating point problems?:
But how can I tell QJsonDocument to use my string as a number
You cannot! Either you store a number, in which case JSON formats the output, or you store it as a string, in which case you do, but then the reading program must be prepared to accept a string and convert to number.
Your code looks as good as it gets. You have the two choices I listed in my first reply.
-
@JonB
I am fine with storing the number as a string. But how can I tell QJsonDocument to use my string as a number, without converting it to a double first? Should I use a custom json parser instead?@Axel-Spoerl
For sake of avoiding confusion I'm only including the related parts of the code and renaming variable names to their type.// Some where in the dialogs constructor ui->lineEdit->setValidator(new QDoubleValidator()); // Some where in a slot float value = m_ui->lineEdit->text().toFloat(); // Some where in the saving json process: QJsonObject json; json["r"] = value; // ... QJsonDocument document(json); QByteArray data = document.toJson();
These are currently the only places the value gets read and used.
When the value of the lineEdit is 13.23, the json in the byte array says:
{ "r": 13.229999542236328, // ... }
-
@JonB
I am fine with storing the number as a string. But how can I tell QJsonDocument to use my string as a number, without converting it to a double first? Should I use a custom json parser instead?@Axel-Spoerl
For sake of avoiding confusion I'm only including the related parts of the code and renaming variable names to their type.// Some where in the dialogs constructor ui->lineEdit->setValidator(new QDoubleValidator()); // Some where in a slot float value = m_ui->lineEdit->text().toFloat(); // Some where in the saving json process: QJsonObject json; json["r"] = value; // ... QJsonDocument document(json); QByteArray data = document.toJson();
These are currently the only places the value gets read and used.
When the value of the lineEdit is 13.23, the json in the byte array says:
{ "r": 13.229999542236328, // ... }
@mhn2 said in How to avoid floating point problems?:
@JonB
I am fine with storing the number as a stringI read this as storing it in the json file as string
-
@mhn2 said in How to avoid floating point problems?:
@JonB
I am fine with storing the number as a stringI read this as storing it in the json file as string
@J-Hilk
The whole point of this conversation is that the OP knows he can store"13.23"
so it outputs like that to the file. Which will be read as a string. Which may not be right for the receiving program. He wants it to output13.23
to the file. Which as he has discovered is not going to happen.... -
-
Well it seems like I either have to accept the floating point problem or use strings to store the number in json.
I will discuss this with my other team members and settle on one of these.
Thanks to everyone (especially JonB) for helping!
@mhn2
My inclination is to store it as a number, and accept the precision. It should not be your job to convert to strings when it's a number. I think you can assume that if (Qt) JSON outputs13.23
this way then any other JSON reader will be convert the13.2299...
back in just the same way. I imagine this is anticipated/standard in JSON data numeric transfer, else everyone else would come up against this issue. -
@J-Hilk
The whole point of this conversation is that the OP knows he can store"13.23"
so it outputs like that to the file. Which will be read as a string. Which may not be right for the receiving program. He wants it to output13.23
to the file. Which as he has discovered is not going to happen....@JonB I seriously fail to conclude that from what was written so far.
Anyway there's still the option of post processing.
Save everything as a String, Mark/Store the stringyfied numbers in a list. parse the previously written json file into memory, remove
"
from marked string/numbers, save the file back. -
@mhn2
My inclination is to store it as a number, and accept the precision. It should not be your job to convert to strings when it's a number. I think you can assume that if (Qt) JSON outputs13.23
this way then any other JSON reader will be convert the13.2299...
back in just the same way. I imagine this is anticipated/standard in JSON data numeric transfer, else everyone else would come up against this issue. -
-
@JonB
Yes it is very likely that the application that will read the json will have the same precision problems, however it might be better to not risk it. Whether or not I should take this risk is something I'm going to have to discuss.@mhn2
Certainly you should discuss. But I believe you are laboring under a misunderstanding/assumption here. You are thinking if you can get the number13.23
or the string"13.23"
into the file that will make it so when the reader fetches it it will convert those digits which make up a string (even if it's stored as a13.23
number the JSON will still have to convert string to number when it reads it) to exactly the binary, floating point representation of13.23
. But it won't, because (I believe) you are showing that the IEEE binary representation of13.23
simply is13.229999542236328
, and that's as close as you going to get whatever you do.