In ob-C.el, line 339 has the format specifier

            (`floatp '("double" "%f"))

to print literal floats. However, that format specifier rounds, which
can result in errors. I think the correct approach is to print the
float without rounding or padding or trying to guess at the desired
accuracy (and let the compiler do whatever is needed):

           (`floatp '("double" "%s"))

Attached is a patch with a test that illustrates the problem (it fails
on HEAD but passes with the patch).


diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 3a6e99623..6b9898ebb 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -339,7 +339,7 @@ FORMAT can be either a format string or a function which is called with VAL."
 	  (pcase basetype
 	    (`integerp '("int" "%d"))
-	    (`floatp '("double" "%f"))
+	    (`floatp '("double" "%s"))
 	      (if (eq org-babel-c-variant 'd) "string" "const char*")
diff --git a/testing/examples/ b/testing/examples/
index c7a96f665..58ace91de 100644
--- a/testing/examples/
+++ b/testing/examples/
@@ -60,6 +60,12 @@
   return 0;
+#+source: float_var
+#+begin_src cpp :var x=1.123456789012345678 :includes "<iostream>" :results silent
+double y = 1.123456789012345678;
+std::cout << (x == y);
 * Array
   :ID:       2df1ab83-3fa3-462a-a1f3-3aef6044a874
diff --git a/testing/lisp/test-ob-C.el b/testing/lisp/test-ob-C.el
index b6dbed8e3..41b500d5b 100644
--- a/testing/lisp/test-ob-C.el
+++ b/testing/lisp/test-ob-C.el
@@ -95,6 +95,13 @@
 		      (org-babel-next-src-block 10)
 		      (should (= 42 (org-babel-execute-src-block))))))
+(ert-deftest ob-C/float-var ()
+  "Test of a string variable"
+  (if (executable-find org-babel-C++-compiler)
+      (org-test-at-id "fa6db330-e960-4ea2-ac67-94bb845b8577"
+		      (org-babel-next-src-block 11)
+		      (should (= 1 (org-babel-execute-src-block))))))
 (ert-deftest ob-C/table ()
   "Test of a table output"
   (if (executable-find org-babel-C++-compiler)

Reply via email to