20 July 2017, 19:35 | #1 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
[vbcc/win] Doing some maths..
Just a newbie exploring C, and trying to write some maths code -- eg to generate a sine table. So nothing to see, here..
I'm using the latest version of vbcc on win64. I've attached the code, the makefile and the exe. This is really about the C translation of the assembler question I asked in this thread: http://eab.abime.net/showthread.php?t=87875 (just read to about post 7 or so; it got really off topic after that ) Code:
#include <math.h> #include <stdio.h> int main (void) { int nrSamples = 256; float deltaAngle = 2.0 * M_PI / ((float) nrSamples); float currAngle, currSine; int iSample; for (iSample = 0; iSample < nrSamples; iSample++) { currAngle = deltaAngle * ((float) iSample); currSine = sin(currAngle); printf("%3d : %f : %f\n", iSample, currAngle, currSine); } return (0); } My ultimate goal is to have this working on this actual hardware, of course. And it doesn't, really; it outputs some unexpected-yet-recognisable results: I'm not sure what's happening there, but it seems like it kind of gets the main part (the mantissa?) right, but then loses it (at the exponent?). I'm cross developing on a PC and I also have Amiga Forever 7, so obviously I'd like to test it there first before I transfer it to the actual amiga (that workflow still needs some work). If I select the default A1200 config, the exe gurues (error #8000 000B). That's unfortunate. If I change the ROM version from 3.1 to 3.X, it works perfectly, and generates exactly the output thomas quoted. So what is going on here? I happen to have 3.X ROMs in my A1200, where it also doesn't really work neither, but at least it doesn't guru. I'm linking to mieee.lib. It says in the vbcc docs (page 83) that this 'will always open MathIeeeSingBas.library, MathIeeeDoubBas.library and MathIeeeDoub-Trans.library'. I don't have MathIeeeSingBas.library in the libs: folder on any of my systems, so maybe that comes with ROM 3.X? Also, tbh, I'm not really sure which printf version I'm calling. Is it the 1 in amiga.lib (http://amigadev.elowar.com/read/ADCD.../node0162.html)? Because according to the docs, this does not support the %f type. Or is it in vc.lib (as the docs slightly seem to suggest)? Including <stdio.h> probably loads the one in targets\m68k-amigaos\include, right? So that's another fine mess I've gotten myself into! |
20 July 2017, 20:28 | #2 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
Btw, if I link to msoft.lib, it works ok on all 3 platforms. Just a whole lot slower, unfortunately..
|
20 July 2017, 20:42 | #3 |
Registered User
Join Date: May 2010
Location: Helsinki, Finland
Posts: 1,341
|
I didn't examine your code very closely but you could rule out the printf() by printing an integer instead, e.g.:
Code:
float angle = 35.3f; printf("%d\n", (int)(angle * 1000.0f)) |
20 July 2017, 20:51 | #4 | |||
Banned
Join Date: Jan 2010
Location: Kansas
Posts: 1,284
|
Frank Wille (phx) could probably give more suggestions and specifics in regards to the ieee libraries but I will start.
Quote:
Quote:
Quote:
|
|||
20 July 2017, 20:51 | #5 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 720
|
I think %f stands for double.
Yeap, just checked https://en.wikipedia.org/wiki/Printf_format_string Try casting float to double in printf's parameters. |
20 July 2017, 21:10 | #6 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
This does indeed give the expected results on 3.X, but still gurues on the default A1200 (3.1). Linking to mieee.lib, btw. So.. what would you say is the problem: the %f printf function (whatever one gets called), or mieee.lib?
|
20 July 2017, 21:19 | #7 | |
Banned
Join Date: Jan 2010
Location: Kansas
Posts: 1,284
|
Quote:
https://en.wikipedia.org/wiki/C_mathematical_functions http://en.cppreference.com/w/c/numeric/math/sin Compiling with vbcc using any c99 features (and probably single precision floats) should use the -c99 command line option! Last edited by matthey; 20 July 2017 at 21:25. |
|
20 July 2017, 21:22 | #8 | |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
Quote:
Code:
printf("%3d : %f : %f\n", iSample, (double) currAngle, (double) currSine); |
|
20 July 2017, 21:35 | #9 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 720
|
|
20 July 2017, 21:43 | #10 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 720
|
hmm, the plot thickens.
float arguments in variadic functions are auto-promoted to double (https://stackoverflow.com/questions/...at-to-a-double ) |
20 July 2017, 21:44 | #11 | |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
Quote:
Code:
#include <math.h> #include <stdio.h> int main (void) { int nrSamples = 256; double deltaAngle = 2.0 * M_PI / ((double) nrSamples); double currAngle, currSine; int iSample; for (iSample = 0; iSample < nrSamples; iSample++) { currAngle = deltaAngle * ((double) iSample); currSine = sin(currAngle); printf("%3d : %f : %f\n", iSample, currAngle, currSine); } return (0); } |
|
20 July 2017, 21:49 | #12 | |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
Quote:
|
|
20 July 2017, 21:59 | #13 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 720
|
sin is not variadic, but the prototype of it should promote the argument to double.
printf is variadic. |
20 July 2017, 22:10 | #14 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
From the tests I've run, neither sin nor printf seems to automatically promote floats to doubles. Which is probably for the best, really. I only get this working when going 'all doubles', as matthey suggested.
|
20 July 2017, 22:12 | #15 | ||
Banned
Join Date: Jan 2010
Location: Kansas
Posts: 1,284
|
Quote:
Quote:
Code:
vc -c99 ... I'm not even sure the mieee.lib supports single precision c99 like features. It should be partially possible (and provide better performance) as the C= ieee libraries have some single precision functions needed by c99. There was no c99 support in the FPU specific libraries for functions like sinf() until I added them because I wanted better c99 support. I didn't touch the mieee.lib which is behind in features and can not really be brought up to the same level of c99 features because of limitations in the C= ieee libraries. Frank could probably tell us exactly what is supported in the mieee.lib but I recommend using only double until we hear from him. |
||
20 July 2017, 22:25 | #16 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
I had this option on already; it doesn't seem to solve the problem, unfortunately.
|
20 July 2017, 22:30 | #17 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 720
|
Ok, further testing.
Works fine in SAS/C and in gcc-6.2.1. Doesn't work in gcc-3.4.0 |
20 July 2017, 22:36 | #18 |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
|
20 July 2017, 22:44 | #19 |
Registered User
Join Date: May 2010
Location: Helsinki, Finland
Posts: 1,341
|
Well as far as outputting garbage goes, I'd say there is something odd going on with the printf function (whichever implementation it is). I don't see any logical reason why a printf would put out non-numeric values like that, even if it did misinterpret the parameters somehow. Maybe that also leads to crashing in some circumstances.
|
20 July 2017, 22:50 | #20 | |
Registered User
Join Date: May 2017
Location: Belgium
Age: 50
Posts: 334
|
Quote:
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Vbcc on Win: Flag <-o> needs string | guy lateur | Coders. C/C++ | 15 | 19 July 2017 20:31 |
Maths library for 68881? | allanmb | Amiga scene | 3 | 29 September 2015 09:59 |
La bosse des maths 5ème | dlfrsilver | request.Old Rare Games | 2 | 12 February 2013 18:23 |
Educational maths title, skateboard? | Nipedley | Looking for a game name ? | 8 | 29 April 2011 21:56 |
Intel representative cant do maths | alexh | Retrogaming General Discussion | 5 | 18 November 2007 23:47 |
|
|