NO

Author Topic: Bug or just a maverick ?  (Read 1765 times)

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 175
    • Bcx Basic to C/C++ Translator
Bug or just a maverick ?
« on: August 23, 2020, 04:48:37 PM »
The sample code (below) when compiled with Pelles v10 produces the following output.

The same code run through MSVC, Clang, Mingw, and LccWin32 does not loose precision.

Pelles
Code: [Select]
1.110999999999
2.221999999999
3.333
4.443999999999
5.554999999999

Others
Code: [Select]
1.111
2.222
3.333
4.444
5.555


Code: [Select]
#include <stdarg.h>       
#include <stdio.h>       
   
 
double ChooseNumber(long nIndex, ...)
{
double Choice = { 0 };
va_list(args);
va_start(args, nIndex);
{
long i;
for (i = 1; i <= nIndex; i += 1)
{
Choice = va_arg(args, double);
if (Choice == 0)
{
goto L1002;
}
if (i == nIndex)
{
goto L1002;
}
}
}
  L1002:;
va_end(args);
return Choice;
}


 

int main(int argc, char *argv[])
{
{
int Index;
for (Index = 1; Index <= 5; Index += 1)
{
printf ("% .15G\n", ChooseNumber (Index, 1.111, 2.222, 3.333, 4.444, 5.555, 0));
}
}
return 0;
}

If I change
                     printf("% .15G\n", .....
to
                     printf("% .12G\n",.....  ( .12 or lower )

then the results match the other compilers output but I think that is a band-aid solution.
« Last Edit: August 24, 2020, 04:49:31 PM by MrBcx »
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Bug or just a maverick ?
« Reply #1 on: August 23, 2020, 09:35:52 PM »
So basically it's Microsoft runtime (of some version) vs Pelles C runtime.

Maybe there is a bug, maybe there isn't. I hardly ever use binary floating-point, but in my experience rounding is just one of many problems with that format.

I will look at this, but it will likely take a while to analyze and reach some kind of conclusion...
/Pelle

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 175
    • Bcx Basic to C/C++ Translator
Re: Bug or just a maverick ?
« Reply #2 on: August 23, 2020, 10:09:16 PM »
So basically it's Microsoft runtime (of some version) vs Pelles C runtime.

Maybe there is a bug, maybe there isn't. I hardly ever use binary floating-point, but in my experience rounding is just one of many problems with that format.

I will look at this, but it will likely take a while to analyze and reach some kind of conclusion...

Thanks Pelle ...

This issue is a little fish in a big pond, so I'm not going to loose any sleep over it.

That was a terribly constructed, mixed-up metaphor.
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Bug or just a maverick ?
« Reply #3 on: August 29, 2020, 03:58:26 PM »
Confirmed - indeed a rounding error somewhere, apparently in the representation of the numbers (printf?):

Code: [Select]
TestP10.exe
 1.110999999999
 2.221999999999
 3.333
 4.443999999999
 5.554999999999

TestP9
 1.111
 2.222
 3.333
 4.444
 5.555

With an int 3 inserted here...
Code: [Select]
_asm int 3;
Choice = va_arg(args, double);
_asm nop;
... I get identical results some steps later:
P9:
Code: [Select]
CPU Disasm
Address              Hex dump                   Command                               Comments
00071076             ³> Ú6A 00                  Úpush 0
00071078             ³. ³DD05 20700700          ³fld qword ptr [77020]                ; float 5.555000000000000
0007107E             ³. ³83EC 08                ³sub esp, 8
00071081             ³. ³DD1C24                 ³fstp qword ptr [esp]
00071084             ³. ³DD05 18700700          ³fld qword ptr [77018]                ; float 4.444000000000000
0007108A             ³. ³83EC 08                ³sub esp, 8
0007108D             ³. ³DD1C24                 ³fstp qword ptr [esp]
00071090             ³. ³DD05 10700700          ³fld qword ptr [77010]                ; float 3.333000000000000
00071096             ³. ³83EC 08                ³sub esp, 8
00071099             ³. ³DD1C24                 ³fstp qword ptr [esp]
0007109C             ³. ³DD05 08700700          ³fld qword ptr [77008]                ; float 2.222000000000000
000710A2             ³. ³83EC 08                ³sub esp, 8
000710A5             ³. ³DD1C24                 ³fstp qword ptr [esp]
000710A8             ³. ³DD05 00700700          ³fld qword ptr [77000]                ; float 1.111000000000000
000710AE             ³. ³83EC 08                ³sub esp, 8
000710B1             ³. ³DD1C24                 ³fstp qword ptr [esp]
000710B4             ³. ³FF75 FC                ³push dword ptr [ebp-4]
000710B7             ³. ³E8 44FFFFFF            ³call 00071000
000710BC             ³. ³83C4 30                ³add esp, 30
000710BF             ³. ³DD5D F0                ³fstp qword ptr [ebp-10]
000710C2             ³. ³DD45 F0                ³fld qword ptr [ebp-10]
000710C5             ³. ³83EC 08                ³sub esp, 8
000710C8             ³. ³DD1C24                 ³fstp qword ptr [esp]
000710CB             ³. ³68 28700700            ³push offset 00077028                 ; ASCII "% .15G",LF
000710D0             ³. ³E8 4B000000            ³call 00071120

P10:
Code: [Select]
CPU Disasm
Address              Hex dump                   Command                               Comments
01391076             ³> Ú6A 00                  Úpush 0
01391078             ³. ³DD05 20803901          ³fld qword ptr [1398020]              ; float 5.555000000000000
0139107E             ³. ³83EC 08                ³sub esp, 8
01391081             ³. ³DD1C24                 ³fstp qword ptr [esp]
01391084             ³. ³DD05 18803901          ³fld qword ptr [1398018]              ; float 4.444000000000000
0139108A             ³. ³83EC 08                ³sub esp, 8
0139108D             ³. ³DD1C24                 ³fstp qword ptr [esp]
01391090             ³. ³DD05 10803901          ³fld qword ptr [1398010]              ; float 3.333000000000000
01391096             ³. ³83EC 08                ³sub esp, 8
01391099             ³. ³DD1C24                 ³fstp qword ptr [esp]
0139109C             ³. ³DD05 08803901          ³fld qword ptr [1398008]              ; float 2.222000000000000
013910A2             ³. ³83EC 08                ³sub esp, 8
013910A5             ³. ³DD1C24                 ³fstp qword ptr [esp]
013910A8             ³. ³DD05 00803901          ³fld qword ptr [1398000]              ; float 1.111000000000000
013910AE             ³. ³83EC 08                ³sub esp, 8
013910B1             ³. ³DD1C24                 ³fstp qword ptr [esp]
013910B4             ³. ³FF75 FC                ³push dword ptr [ebp-4]
013910B7             ³. ³E8 44FFFFFF            ³call 01391000
013910BC             ³. ³83C4 30                ³add esp, 30
013910BF             ³. ³DD5D F0                ³fstp qword ptr [ebp-10]
013910C2             ³. ³DD45 F0                ³fld qword ptr [ebp-10]
013910C5             ³. ³83EC 08                ³sub esp, 8
013910C8             ³. ³DD1C24                 ³fstp qword ptr [esp]
013910CB             ³. ³68 28803901            ³push offset 01398028                 ; ASCII "% .15G",LF
013910D0             ³. ³E8 4B000000            ³call 01391120
« Last Edit: August 29, 2020, 04:22:43 PM by jj2007 »