public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110749] New: wrong show float
@ 2023-07-20  6:46 alexsyrezerv at mail dot ru
  2023-07-20  7:31 ` [Bug c++/110749] " schwab@linux-m68k.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: alexsyrezerv at mail dot ru @ 2023-07-20  6:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

            Bug ID: 110749
           Summary: wrong show float
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexsyrezerv at mail dot ru
  Target Milestone: ---

Created attachment 55586
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55586&action=edit
To try on different compillers

Code:
```
#include <iostream>
#include <iomanip>

void func_float_point(std::ostream& os, const double& value, const std::size_t&
size) {
  os << (0.0 > value ? '-' : '0') << std::setw(size - 1) << std::setfill('0')
<< std::abs(value);
}

int main(int, char**) {
  const double a[] = {90.8425, 101.9225};
  for(std::size_t i = 0; i < 2; ++i){
    func_float_point(std::cout, a[i], 17);
    std::cout << std::endl;
  }

  return 0;
}
```
result
```
butiaev@butiaev-VirtualBox:~/tmp$ make bug
g++     bug.cpp   -o bug
butiaev@butiaev-VirtualBox:~/tmp$ ./bug
000000000090.8425
0000000000101.922
butiaev@butiaev-VirtualBox:~/tmp$

```

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
@ 2023-07-20  7:31 ` schwab@linux-m68k.org
  2023-07-20  9:09 ` alexsyrezerv at mail dot ru
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: schwab@linux-m68k.org @ 2023-07-20  7:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
101.9225 cannot be represented exactly, the nearest representable value is
101.92249999999999943.

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
  2023-07-20  7:31 ` [Bug c++/110749] " schwab@linux-m68k.org
@ 2023-07-20  9:09 ` alexsyrezerv at mail dot ru
  2023-07-20 10:03 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: alexsyrezerv at mail dot ru @ 2023-07-20  9:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #2 from Aleksei <alexsyrezerv at mail dot ru> ---
Created attachment 55589
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55589&action=edit
GDB SHOW IT

But GDB show it normally

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
  2023-07-20  7:31 ` [Bug c++/110749] " schwab@linux-m68k.org
  2023-07-20  9:09 ` alexsyrezerv at mail dot ru
@ 2023-07-20 10:03 ` redi at gcc dot gnu.org
  2023-07-20 10:58 ` alexsyrezerv at mail dot ru
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-20 10:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Your program is using the default precision for printing a double: 6
significant figures.

101.92249999999999943 with 6 digits of precision is 101.922 (because (24 rounds
to 2).

101.92249999999999943 with 7 digits of precision is 101.9225 (because 49 rounds
to 5).

If you use std::setprecision(7) << std::abs(value) you will get 101.9225:

000000000090.8425
000000000101.9225

If you use std::setprecision(17) you will get the same values as GDB shows:

090.842500000000001
000000000101.9225

If you use std::setprecision(18) you'll see that Andreas is correct:

090.8425000000000011
0101.922499999999999


This is not a GCC bug, this is just how floating-point numbers work.

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
                   ` (2 preceding siblings ...)
  2023-07-20 10:03 ` redi at gcc dot gnu.org
@ 2023-07-20 10:58 ` alexsyrezerv at mail dot ru
  2023-07-20 11:08 ` alexsyrezerv at mail dot ru
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: alexsyrezerv at mail dot ru @ 2023-07-20 10:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #4 from Aleksei <alexsyrezerv at mail dot ru> ---
ок

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
                   ` (3 preceding siblings ...)
  2023-07-20 10:58 ` alexsyrezerv at mail dot ru
@ 2023-07-20 11:08 ` alexsyrezerv at mail dot ru
  2023-07-20 12:10 ` redi at gcc dot gnu.org
  2023-07-20 12:10 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: alexsyrezerv at mail dot ru @ 2023-07-20 11:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #5 from Aleksei <alexsyrezerv at mail dot ru> ---
this variant more clear.. 4 symbol after point// may by c++ has modification
for set symbols after decimal point ? 
```
  void func_float_point(std::ostream& os, const double& value, const
mcx::convert_info_t& cv) {
    char buf[buf_size];
    snprintf(buf, buf_size - 1, "%0*.*f", cv.size, cv.pos, value);
    os << buf;
  }
```

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
                   ` (4 preceding siblings ...)
  2023-07-20 11:08 ` alexsyrezerv at mail dot ru
@ 2023-07-20 12:10 ` redi at gcc dot gnu.org
  2023-07-20 12:10 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-20 12:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
If you use std::fixed as the floatfield format then std::setprecision specifies
the number of digits after the decimal point:

os << (0.0 > value ? '-' : '0') << std::setw(cv.size - 1) << std::setfill('0')
<< std::setprecision(cv.pos) << std::fixed << std::abs(value);


You don't need to handle the -'- sign yourself:

os << std::setw(cv.size) << std::internal << std::setfill('0') << std::fixed <<
std::setprecision(cv.pos) << value;


This matches the output of printf("%0*.*f", cv.size, cv.pos, value)

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

* [Bug c++/110749] wrong show float
  2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
                   ` (5 preceding siblings ...)
  2023-07-20 12:10 ` redi at gcc dot gnu.org
@ 2023-07-20 12:10 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-20 12:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110749

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #6)
> You don't need to handle the -'- sign yourself:

Oops, I mean '-' sign.

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

end of thread, other threads:[~2023-07-20 12:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-20  6:46 [Bug c++/110749] New: wrong show float alexsyrezerv at mail dot ru
2023-07-20  7:31 ` [Bug c++/110749] " schwab@linux-m68k.org
2023-07-20  9:09 ` alexsyrezerv at mail dot ru
2023-07-20 10:03 ` redi at gcc dot gnu.org
2023-07-20 10:58 ` alexsyrezerv at mail dot ru
2023-07-20 11:08 ` alexsyrezerv at mail dot ru
2023-07-20 12:10 ` redi at gcc dot gnu.org
2023-07-20 12:10 ` redi at gcc dot gnu.org

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