help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is 0.01 here not 0.01 here 0.009999999999999?


From: Jean Louis
Subject: What is 0.01 here not 0.01 here 0.009999999999999?
Date: Fri, 02 Apr 2021 16:42:42 +0300

I would like to get a number increased for 0.01:

(defun rcd-vc-increase-decimal-revision-number (nn.nn)
  (let* ((nn.nn (format "%s" nn.nn))
         (nn.nn (format "%.2f" (string-to-number nn.nn)))
         (nn.nn (string-to-number nn.nn)))
    (+ nn.nn 0.01)))

But result is not what I want, as I want to get 10.13, though it is
tiny loss:

(rcd-vc-increase-decimal-revision-number "10.12") → 10.129999999999999

I found how to solve it, but maybe there is better way to round the
decimals behind the dot?

(defun rcd-vc-increase-decimal-revision-number (nn.nn)
  (let* ((nn.nn (format "%s" nn.nn))
         (nn.nn (format "%.2f" (string-to-number nn.nn)))
         (nn.nn (string-to-number nn.nn)))
    (string-to-number (format "%.2f" (+ nn.nn 0.01)))))

(rcd-vc-increase-decimal-revision-number "10.12") → 10.13


Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://rms-support-letter.github.io/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]