From: Austin Ziegler Date: 2019-01-17T15:14:47-05:00 Subject: [ruby-core:91143] Re: [Ruby trunk Bug#15544] sprintf returns an unexpected string --===============1900576998== Content-Type: multipart/alternative; boundary="000000000000d01ffe057fad0c7a" --000000000000d01ffe057fad0c7a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable #include int main(int argc, char *argv[]) { printf("%.16g\n", 8.78); } compile and run this and you will get the same result. The problem is floating-point values cannot be represented in binary exactly. https://developers.slashdot.org/story/10/05/02/1427214/what-every-programme= r-should-know-about-floating-point-arithmetic -a On Thu, Jan 17, 2019 at 2:52 PM wrote: > Issue #15544 has been reported by areman01 (Andr=C3=A9 Kullmann). > > ---------------------------------------- > Bug #15544: sprintf returns an unexpected string > https://bugs.ruby-lang.org/issues/15544 > > * Author: areman01 (Andr=C3=A9 Kullmann) > * Status: Open > * Priority: Normal > * Assignee: > * Target version: > * ruby -v: ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux] > * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN > ---------------------------------------- > Hello, > > I've a little problem with the sprintf function. It returns unexpected > results ( in my opinion ) > > ``` ruby > sprintf("%.16g", 8.78) > # returns "8.779999999999999" > # my expected result is "8.78" > ``` > > Can somebody explain this behavior to me ? > > Best Begards > > > > -- > https://bugs.ruby-lang.org/ > > Unsubscribe: > > --=20 Austin Ziegler =E2=80=A2 halostatue@gmail.com =E2=80=A2 austin@halostatue.c= a http://www.halostatue.ca/ =E2=80=A2 http://twitter.com/halostatue --000000000000d01ffe057fad0c7a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

#include <stdio.h>


int main(int argc, char *argv[]) {

=C2=A0 printf("%.16g\n", 8.78);

}


compile and run this and you will get the same result.


The problem is floati= ng-point values cannot be represented in binary exactly.


https://developers.slashdot.org/story/10/05/02/14= 27214/what-every-programmer-should-know-about-floating-point-arithmetic=


-a


On Thu, Jan = 17, 2019 at 2:52 PM <an= dre.kullmann@googlemail.com> wrote:
Issue = #15544 has been reported by areman01 (Andr=C3=A9 Kullmann).

----------------------------------------
Bug #15544: sprintf returns an unexpected string
https://bugs.ruby-lang.org/issues/15544

* Author: areman01 (Andr=C3=A9 Kullmann)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Hello,

I've a little problem with the sprintf function. It returns unexpected = results ( in my opinion )

``` ruby
sprintf("%.16g", 8.78)
# returns "8.779999999999999"
# my expected result is "8.78"
```

Can somebody explain this behavior to me ?

Best Begards



--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=3Dunsubscribe= >
<http://lists.ruby-lang.org/cgi-bin/m= ailman/options/ruby-core>


--
--000000000000d01ffe057fad0c7a-- --===============1900576998== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Unsubscribe: --===============1900576998==--