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

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

Re: What is 0.01 here not 0.01 here 0.009999999999999?


From: Jean Louis
Subject: Re: What is 0.01 here not 0.01 here 0.009999999999999?
Date: Fri, 2 Apr 2021 20:03:54 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Stefan Monnier <monnier@iro.umontreal.ca> [2021-04-02 16:56]:
> > I would like to get a number increased for 0.01:
> 
> Are you aware that in most programming languages (including ELisp) there
> is no floating point number whose value is exactly 0.01?
> 
> If not, I suggest you either avoid floating point numbers, accept that
> floating point numbers always are inexact, or read up on floating point
> numbers (e.g. https://en.wikipedia.org/wiki/Floating-point_arithmetic)

Thank you. I did browse it, did not yet understand it.

Not that I need the absolute internally, just the practical
result as we learned it in school like that 10.11 plus 0.01
results with 10.12 and not something else. Results are used for
automated version numbers (in some cases).

Sometimes result is just as expected:

(+ 10.01 0.01) → 10.02

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

It does not happen always:

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

Perl also says it is "about" or ~10.13

$ perl 
print 10.12+0.01;
10.13~

Guile also:

scheme@(guile-user)> (+ 10.12 0.01)
$1 = 10.129999999999999

So it is not always, it is weird, without reading scientific
papers. Thank you Eli. But I have solved it for my specific need
for RCD Version Control System this way:

(defun string-is-number-p (s)
  (let ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
          ((string-match "[^0123456789\\.]" s) nil)
          ((numberp (string-to-number s)) (string-to-number s)))))

(defun rcd-vc-revision-is-floating-number-p (revision)
  "Return T if REVISION is possibly floating number."
  (if (string-is-number-p (format "%s" revision))
      (let* ((nnnn (split-string (format "%.2f" (string-to-number (format "%s" 
revision))) "\\."))
             (two (length nnnn)))
        (when (= two 2)
          (let* ((first-is-number (string-is-number-p (car nnnn)))
                 (second-is-number (string-is-number-p (cadr nnnn))))
            (when (and first-is-number second-is-number)
              t))))
    nil))

(defun rcd-vc-increase-decimal-revision-number (nn.nn)
  "Increase the floating number NN.NN provided either as number or
string for 0.01."
  (if (rcd-vc-revision-is-floating-number-p 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)))
        (format "%.2f" (+ nn.nn 0.01)))
    nn.nn))

This I want to work as here:
(rcd-vc-increase-decimal-revision-number "Version 1.0") → "Version 1.0"

But this I want to get incremented, converted into string, even
if string given:

(rcd-vc-increase-decimal-revision-number "0") → "0.01"
(rcd-vc-increase-decimal-revision-number "1") → "1.01"
(rcd-vc-increase-decimal-revision-number "11.12") → "11.13"
(rcd-vc-increase-decimal-revision-number 11.12) → "11.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]