-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
failure to load highly precise floats #150
Comments
with latest master I get the same issue, except the error is a bit more user friendly: |
It seems that those values are outside the bounds allowed by floats, but C allows you to save them in float variables anyway. so this could be seen as a user error - I'm passing bad values. but my program receives some values from other libraries and from the user and I still don't want it to break when trying to load these values back. so I tried this and it fixes my problem: diff --git a/src/save.c b/src/save.c
index 52ff1f4..7c47c23 100644
--- a/src/save.c
+++ b/src/save.c
@@ -16,6 +16,7 @@
#include <stdbool.h>
#include <assert.h>
#include <limits.h>
+#include <float.h>
#include <yaml.h>
@@ -552,6 +553,15 @@ static const char * cyaml__get_float(
{
static char string[64];
+ /* clamp */
+ if (value <= FLT_MIN)
+ {
+ value = 1e-37;
+ }
+ else if (value >= FLT_MAX)
+ {
+ value = 1e+37;
+ }
sprintf(string, "%g", value);
return string; maybe you can do it in a more tidy way unless you have a better solution. I think since cyaml allows you to save such a value, it should also not have a problem loading it back. my suggestion is to just log a warning that the value was out of bounds and clamp it like above EDIT: I guess you could just use |
another idea is to allow user overrides or callbacks before saving so I could clamp the value in my struct (or do any other arbitrary validation): #120 |
Force use of libcyaml subproject. Use patched version that fixes an issue with loading out-of-bounds floats. See tlsa/libcyaml#150 for more info.
Please test with #151 The old behavior of rejecting floating point values that overflow or underflow can be achieved by setting |
just tried it, this fixes the issue. thanks! |
Thanks, merged. |
when you have a struct with only a float field inside and you set the value to 1.55331e-40f and attempt to save it, it works. it gets printed as:
when you try to load it however it rejects it:
The text was updated successfully, but these errors were encountered: