From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: What is 0.01 here not 0.01 here 0.009999999999999? Date: Fri, 2 Apr 2021 20:03:54 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2045"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0.6 (2021-03-06) Cc: help-gnu-emacs@gnu.org To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Apr 02 19:08:32 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSNHb-0000RJ-EA for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 19:08:31 +0200 Original-Received: from localhost ([::1]:58864 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSNHa-0005wn-FT for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 13:08:30 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38668) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSNH2-0005we-GB for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 13:07:56 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:57163) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSNH0-00083X-6K for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 13:07:56 -0400 Original-Received: from localhost ([::ffff:41.202.241.42]) (AUTH: PLAIN securesender, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 000000000001E1D1.0000000060674F66.00005251; Fri, 02 Apr 2021 10:07:49 -0700 Mail-Followup-To: Stefan Monnier , help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, LOTS_OF_MONEY=0.001, MONEY_FROM_41=1.999, MONEY_NOHTML=0.429, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128813 Archived-At: * Stefan Monnier [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/