Giter Site home page Giter Site logo

Comments (3)

dearblue avatar dearblue commented on July 17, 2024

For information, it seems also to have the same problem with the original, https://github.com/mattn/strtod.
Adding values in 0.1 intervals from 0.2 to 0.9 and reporting only failures, the results are shown below.

%  cc strtod.c -lm && ./a.out
CASE: .3
  ERR: .3, No error: 0
    E1 0.300000, 0.3, , 0
    E2 0.300000, 0.3, , 0
different value

CASE: .6
  ERR: .6, No error: 0
    E1 0.600000, 0.6, , 0
    E2 0.600000, 0.6, , 0
different value

CASE: .7
  ERR: .7, No error: 0
    E1 0.700000, 0.7, , 0
    E2 0.700000, 0.7, , 0
different value

CASE: 2.2250738585072010e-308
  ERR: 2.2250738585072010e-308, Result too large
    E1 0.000000, 0, , 34
    E2 0.000000, 2.22507e-308, , 34
different value

CASE: 2.2250738585072011e-308
  ERR: 2.2250738585072011e-308, Result too large
    E1 0.000000, 0, , 34
    E2 0.000000, 2.22507e-308, , 34
different value
patch for test of strtod.c
diff --git a/strtod.c b/strtod.c
index bd5378b..42bb574 100644
--- a/strtod.c
+++ b/strtod.c
@@ -148,8 +148,6 @@ test(char* str)
     char *e1, *e2;
     int x1, x2;
 
-    printf("CASE: %s\n", str);
-
     errno = 0;
     e1 = NULL;
     d1 = vim_strtod(str, &e1);
@@ -161,22 +159,29 @@ test(char* str)
     x2 = errno;
 
     if (d1 != d2 || e1 != e2 || x1 != x2) {
+        printf("CASE: %s\n", str);
 	printf("  ERR: %s, %s\n", str, strerror(errno));
 	printf("    E1 %f, %g, %s, %d\n", d1, d1, e1 ? e1 : "", x1);
 	printf("    E2 %f, %g, %s, %d\n", d2, d2, e2 ? e2 : "", x2);
        if (d1 != d2) puts("different value");
        if (e1 != e2) puts("different end position");
        if (x1 != x2) puts("different errno");
-    } else {
-	printf("  SUCCESS [%f][%s]: %s\n", d1, e1 ? e1 : "", strerror(errno));
+        printf("\n");
     }
-    printf("\n");
 }
 
     int
 main()
 {
     test(".1");
+    test(".2");
+    test(".3");
+    test(".4");
+    test(".5");
+    test(".6");
+    test(".7");
+    test(".8");
+    test(".9");
     test("  .");
     test("  1.2e3");
     test(" +1.2e3");

from mruby.

matz avatar matz commented on July 17, 2024

For several reasons, I recommend mruby-marshal to use mrb_read_float() instead of strtod().

  • strtod() uses locale which is fragile (for example we need to use 0,3 not 0.3 for some locale)
  • it is very hard to implement bit-to-bit equality with independent string-to-float functions

For the same reason, I recommend to use mrb_float_to_str() instead of printf().

from mruby.

dearblue avatar dearblue commented on July 17, 2024

Understood. Thank you very much.
I tried that too, but it's not possible.

I think it makes sense to write it in doc/limitations.md as it is now, so I'll update that one.

from mruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.