English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 25 July 2018, 16:31   #1
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
VBCC - What's going on here?

I'm having a weird problem. The code below shows it. Sometimes cos(M_PI / 2) is giving 0.0 instead of 1.0. Or, more specifically, the assignment of the function call's result seems to be wrong, as if I call the function by itself it's fine.

I'm either doing something really silly, or I've broken my system somehow, or I've hit a compiler bug.

Can anybody help?

Code:
#include <math.h>
#include <stdio.h>

int main(int argc, char const *argv[]) {
    double a = M_PI / 2;
    double x = cos(a);
    double y = sin(a);
    printf("%f: %f, %f\n", a, x, y);
    printf("%f\n", cos(M_PI / 2));
    printf("%f\n", sin(M_PI / 2));
    return 0;
}
When I run this on an (emulated) Amiga I get:

1.570796: 0.000000, 0.000000
0.000000
1.000000

When I run it on a Linux machine I get the correct result:

1.570796: 0.000000, 1.000000
0.000000
1.000000

Last edited by deimos; 25 July 2018 at 17:06.
deimos is offline  
Old 25 July 2018, 17:17   #2
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
You run it on real hardware or emulation?
If on emulation in what emulated amiga? Have you tried running the executable in another emulated amiga? (i.e. if you run it in a500+ try it in A1200)

In my experience (in emulation) the #?math#? libraries in libs: are usually to blame.

Last edited by alkis; 25 July 2018 at 17:18. Reason: typo
alkis is offline  
Old 25 July 2018, 17:34   #3
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
It's running on an emulated A1000 with KS / WB 2.04, and I'm cross-compiling from Windows.

I'll try setting up a different emulated machine, but I think if it was a problem there then the machine would be crashing all over the place.

EDIT

Well, that's very interesting. The code works on an emulated A3000, booted from the same Workbench that I installed on my emulated A1000 and given the same second hard disk so I could run the exact same executable.

Now, the questions... Why? And how can I fix it? Are the libraries that come with vbcc no good for standard 68000 machines, or am I doing something wrong? Why is the code able to give the right answer in some circumstances and not others?

Last edited by deimos; 25 July 2018 at 17:49. Reason: More info
deimos is offline  
Old 25 July 2018, 17:52   #4
asymetrix
Registered User

 
Join Date: Jul 2009
Location: UK
Posts: 93
Different compilers would give different results, especially when Amiga has not the same int as x86 int size, we all should be using the standard int 32 and double data types. Dividing using an int into a resulting double also has rounding issues.
asymetrix is offline  
Old 25 July 2018, 17:59   #5
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
I'm running the exact same executable on the two emulated Amigas, so different compilers don't come into it. And it's not just a case of getting different results, one of the results is wrong.
deimos is offline  
Old 25 July 2018, 18:08   #6
PeterK
Registered User
 
Join Date: Apr 2005
Location: Hangover
Posts: 2,407
If you are running WinUAE you could try to enable the FPU for the 68000 configuration by unticking "More compatible" for the CPU and enabling the 68882 FPU. I don't know if it's possible to replace the OS 2.x MathLibs with newer versions from OS 3.x.

It could also be a Jit / non-Jit problem, because the Jit starts compiling with a delay of 5 passes in a loop.
PeterK is offline  
Old 25 July 2018, 18:17   #7
Photon
Moderator
Photon's Avatar
 
Join Date: Nov 2004
Location: Hult / Sweden
Posts: 4,591
Too many factors. Always try with the Default Quickstart config first (A500 or A1200). If you change emulation settings, you must know what they affect. If you select a different KS/WB config, you must set up your Workbench correctly according to how the real machine should be set up for things to work.

Tempted to move this to Support.WinUAE, but I will wait and see where this goes. Maybe it's a (non-native) C/C++ problem.
Photon is offline  
Old 25 July 2018, 18:20   #8
asymetrix
Registered User

 
Join Date: Jul 2009
Location: UK
Posts: 93
We have different Amiga hardware, different kickstarts, different APIs, mixed versions, emulated - not emulated, cross compiled spaghetti soup nightmare. That's why things slow down development on Amiga. We really should not even have access to internal data structures. We should be all using common Api function that's abstracted across all systems.
asymetrix is offline  
Old 25 July 2018, 18:31   #9
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
Heh, incidentally I also have math problem with 2.04.

https://github.com/bebbo/gcc/issues/35

My bet is on the math#?.libraries in libs:
alkis is offline  
Old 25 July 2018, 18:53   #10
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
The thing is, it's a 10 line C program that uses only standard C types and standard C functions.

If it was a UAE problem then I'm certain it would show up elsewhere.

If there was a problem with my C code then someone would have spotted it by now.

I'm sure the only move away in my UAE config from 100% compatible is to enable a real time clock, so I don't think that is to blame, or could be. The UAE config was cleanly installed recently and has only been used to test code I'm writing - I have not randomly fiddled with it.

I think the most likely cause is the C libraries that my code is linked to, which would probably wrap the Amiga libraries in libs, rather than the Amiga libraries themselves, but I don't know how to prove that.
deimos is offline  
Old 25 July 2018, 18:56   #11
PeterK
Registered User
 
Join Date: Apr 2005
Location: Hangover
Posts: 2,407
@alkis
But in your github discussion it sounds as if you have fixed the problem after a compiler update by a make clean or updating some compiler libs before recompiling it again.

Btw, the OS 2.x mathlibs are the same as those in OS 3.1, only 3.9 has newer mathlibs.

Last edited by PeterK; 25 July 2018 at 19:01.
PeterK is offline  
Old 25 July 2018, 19:05   #12
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
Quote:
Originally Posted by PeterK View Post
@alkis
But in your github discussion it sounds as if you have fixed the problem after a compiler update by a make clean or updating some compiler libs before recompiling it again.

Btw, the OS 2.x mathlibs are the same as those in OS 3.1, only 3.9 has newer mathlibs.
Yes, all went well after rebuilding the toolchain. Except on A500+.

I just copied the libs:math#? from 3.1 to 2.04 and now it works in A500+ for me
alkis is offline  
Old 25 July 2018, 19:10   #13
PeterK
Registered User
 
Join Date: Apr 2005
Location: Hangover
Posts: 2,407
Quote:
Originally Posted by alkis View Post
I just copied the libs:math#? from 3.1 to 2.04 and now it works in A500+ for me
That's interesting, because all the math libs of OS 3.1 are still v37 or v38, but I don't know the versions of OS 2.04. Maybe there were some late bugfixes in OS 2.1 mathieeedoubbas.library v38.2 ?

Last edited by PeterK; 25 July 2018 at 19:18.
PeterK is offline  
Old 25 July 2018, 19:26   #14
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
Could someone maybe try my code? If things were really as broken as they seem then nothing would work, therefore I must be doing something wrong, and I need someone to tell me the right way to do it.
deimos is offline  
Old 25 July 2018, 19:29   #15
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
just attach your executable so we can run it.
alkis is offline  
Old 25 July 2018, 19:33   #16
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
I think I need someone to tell my how to correctly compile and link the code (after verifying that it's correct).

I'd also attach my current executable, but I don't see how.

EDIT, I see I can if I "go advanced", but only certain file extensions are allowed. I don't think the mods want me to upload executables.
deimos is offline  
Old 25 July 2018, 19:36   #17
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
Reply, Go Advanced, press the little clip icon to the right of the font settings on top.
alkis is offline  
Old 25 July 2018, 19:40   #18
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
Zipped executable attached.
Attached Files
File Type: zip bug.zip (5.9 KB, 13 views)
deimos is offline  
Old 25 July 2018, 19:43   #19
deimos
Registered User

 
Join Date: Jul 2018
Location: Londonish / UK
Posts: 111
Exe with source code, makefile, etc.
Attached Files
File Type: zip bug-inc-src.zip (7.2 KB, 17 views)
deimos is offline  
Old 25 July 2018, 19:45   #20
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 456
Works as designed in A500+, A1200.

I reverted my math#? libs (I kept a backup) and rerun on A500+ and gives the bug you describe in first post.

It's the libs.
alkis is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
vbcc 0.9f released phx News 49 01 April 2018 12:33
vbcc V0.9e released phx News 17 31 October 2016 22:18
VBCC and #include majikeyric Coders. C/C++ 3 03 March 2016 16:07
vbcc 0.9d phx News 43 13 July 2015 20:41
VBCC 0.8j for Windows hitchhikr Coders. General 11 09 October 2008 01:58

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 15:22.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
Page generated in 0.09135 seconds with 14 queries