emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] float format specifier in ob-C.el
@ 2023-05-31 15:36 Leo Butler
  2023-06-01  8:27 ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Leo Butler @ 2023-05-31 15:36 UTC (permalink / raw)
  To: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 513 bytes --]

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).

Leo




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-On-ltb-ob-max-ob-C-float-format.patch --]
[-- Type: text/x-diff; name="0001-On-ltb-ob-max-ob-C-float-format.patch", Size: 1619 bytes --]

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."
 	 (type
 	  (pcase basetype
 	    (`integerp '("int" "%d"))
-	    (`floatp '("double" "%f"))
+	    (`floatp '("double" "%s"))
 	    (`stringp
 	     (list
 	      (if (eq org-babel-c-variant 'd) "string" "const char*")
diff --git a/testing/examples/ob-C-test.org b/testing/examples/ob-C-test.org
index c7a96f665..58ace91de 100644
--- a/testing/examples/ob-C-test.org
+++ b/testing/examples/ob-C-test.org
@@ -60,6 +60,12 @@
   return 0;
 #+end_src
 
+#+source: float_var
+#+begin_src cpp :var x=1.123456789012345678 :includes "<iostream>" :results silent
+double y = 1.123456789012345678;
+std::cout << (x == y);
+#+end_src
+
 * Array
   :PROPERTIES:
   :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)

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] float format specifier in ob-C.el
  2023-05-31 15:36 [PATCH] float format specifier in ob-C.el Leo Butler
@ 2023-06-01  8:27 ` Ihor Radchenko
  2023-06-01 19:00   ` Leo Butler
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2023-06-01  8:27 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

> 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"))

Makes sense. Thanks!

> -	    (`floatp '("double" "%f"))
> +	    (`floatp '("double" "%s"))

Please add comment explaining why not %f. Otherwise, it is not fully
clear why "%s" is used.

> --- a/testing/examples/ob-C-test.org
> +++ b/testing/examples/ob-C-test.org
> @@ -60,6 +60,12 @@
>    return 0;
>  #+end_src
>  
> +#+source: float_var
> +#+begin_src cpp :var x=1.123456789012345678 :includes "<iostream>" :results silent
> +double y = 1.123456789012345678;
> +std::cout << (x == y);
> +#+end_src

Please move the source block directly into the test using
`org-test-with-temp-text'. The test will be more readable then.

`org-test-at-id' should better be used only when necessary in order to
not jump back and forth when debugging tests.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] float format specifier in ob-C.el
  2023-06-01  8:27 ` Ihor Radchenko
@ 2023-06-01 19:00   ` Leo Butler
  2023-06-02  7:25     ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Leo Butler @ 2023-06-01 19:00 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 1431 bytes --]

On Thu, Jun 01 2023, Ihor Radchenko <yantar92@posteo.net> wrote:

> Leo Butler <Leo.Butler@umanitoba.ca> writes:
>
>> 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"))
>
> Makes sense. Thanks!
>
>> -	    (`floatp '("double" "%f"))
>> +	    (`floatp '("double" "%s"))
>
> Please add comment explaining why not %f. Otherwise, it is not fully
> clear why "%s" is used.

Done.

>
>> --- a/testing/examples/ob-C-test.org
>> +++ b/testing/examples/ob-C-test.org
>> @@ -60,6 +60,12 @@
>>    return 0;
>>  #+end_src
>>  
>> +#+source: float_var
>> +#+begin_src cpp :var x=1.123456789012345678 :includes "<iostream>" :results silent
>> +double y = 1.123456789012345678;
>> +std::cout << (x == y);
>> +#+end_src
>
> Please move the source block directly into the test using
> `org-test-with-temp-text'. The test will be more readable then.
>
> `org-test-at-id' should better be used only when necessary in order to
> not jump back and forth when debugging tests.

Ok, I have made the changes requested. Please see the attached patch.

Leo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ob-C.el-replace-f-with-s-to-prevent-unneeded-ro.patch --]
[-- Type: text/x-diff; name="0001-lisp-ob-C.el-replace-f-with-s-to-prevent-unneeded-ro.patch", Size: 2001 bytes --]

From 65bddc861c1f439e98d5764f0587e97d4d99a190 Mon Sep 17 00:00:00 2001
From: Leo Butler <leo.butler@umanitoba.ca>
Date: Thu, 1 Jun 2023 13:39:13 -0500
Subject: [PATCH] lisp/ob-C.el: replace %f with %s to prevent unneeded rounding

* lisp/ob-C.el (org-babel-C-val-to-C-type): Floats should be printed
as string literals to prevent rounding introduced by %f format.

* testing/lisp/test-ob-C.el (ob-C/float-var): Test that floats are not
rounded when passed as an org :var.
---
 lisp/ob-C.el              |  2 +-
 testing/lisp/test-ob-C.el | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 3a6e99623..7763c4c07 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."
 	 (type
 	  (pcase basetype
 	    (`integerp '("int" "%d"))
-	    (`floatp '("double" "%f"))
+	    (`floatp '("double" "%s")) ;; %f rounds, use %s to print the float literally
 	    (`stringp
 	     (list
 	      (if (eq org-babel-c-variant 'd) "string" "const char*")
diff --git a/testing/lisp/test-ob-C.el b/testing/lisp/test-ob-C.el
index b6dbed8e3..8546a48dd 100644
--- a/testing/lisp/test-ob-C.el
+++ b/testing/lisp/test-ob-C.el
@@ -95,6 +95,17 @@
 		      (org-babel-next-src-block 10)
 		      (should (= 42 (org-babel-execute-src-block))))))
 
+(ert-deftest ob-C/float-var ()
+  "Test that floats are passed without unnecessary rounding."
+  (if (executable-find org-babel-C++-compiler)
+      (org-test-with-temp-text 
+"#+source: float_var
+#+begin_src cpp :var x=1.123456789012345678 :includes \"<iostream>\" :results silent
+double y = 1.123456789012345678;
+std::cout << (x == y);
+#+end_src"
+(should (= 1 (org-babel-execute-src-block))))))
+
 (ert-deftest ob-C/table ()
   "Test of a table output"
   (if (executable-find org-babel-C++-compiler)

base-commit: 5b4eebfabdc6a9b4ed223025161a342cb312c1d0
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] float format specifier in ob-C.el
  2023-06-01 19:00   ` Leo Butler
@ 2023-06-02  7:25     ` Ihor Radchenko
  2023-06-02 13:50       ` Leo Butler
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2023-06-02  7:25 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

> Ok, I have made the changes requested. Please see the attached patch.

Thanks!
Applied, onto bugfix.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=d55a11214

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] float format specifier in ob-C.el
  2023-06-02  7:25     ` Ihor Radchenko
@ 2023-06-02 13:50       ` Leo Butler
  0 siblings, 0 replies; 5+ messages in thread
From: Leo Butler @ 2023-06-02 13:50 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Org Mode Mailing List

On Fri, Jun 02 2023, Ihor Radchenko <yantar92@posteo.net> wrote:

> Thanks!
> Applied, onto bugfix.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=d55a11214

Thanks, Ihor.
Leo

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-06-02 13:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-31 15:36 [PATCH] float format specifier in ob-C.el Leo Butler
2023-06-01  8:27 ` Ihor Radchenko
2023-06-01 19:00   ` Leo Butler
2023-06-02  7:25     ` Ihor Radchenko
2023-06-02 13:50       ` Leo Butler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).