RetroBSD

2.11BSD operating system for microcontrollers
It is currently Tue Dec 11, 2018 7:47 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun Sep 27, 2015 2:58 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Hi, I've tried with floating point - 9 degree test.

Code:
// 9 degree TRIGONOMETRIC functions forensic calculator test
// TRIG9.c
// Simulates entering asin(acos(atan(tan(cos(sin(9))))))
//   (or pressing 9 sin cos tan atan acos asin )
//    into a calculator working in DEG mode.
// More results at:
// http://www.rskey.org/~mwsebastian/miscprj/forensics.htm
// Compiled under SmallerC
// Pito 9/2015

#include <stdio.h>

#define FPI      0x40490FDB

// 32-bit soft float
int __divsf3(int, int);
int __subsf3(int, int);
int __mulsf3(int, int);
int __addsf3(int, int);

int pow(int, int);
int sin(int);
int cos(int);
int tan(int);
int asin(int);
int acos(int);
int atan(int);

int __fixsfsi(int);
int __floatsisf(int);

#define POW pow
#define SIN sin
#define COS cos
#define TAN tan
#define ASN asin
#define ACS acos
#define ATN atan

#define DIV __divsf3
#define SUB __subsf3
#define MUL __mulsf3
#define ADD __addsf3
#define I2F __floatsisf
#define F2I __fixsfsi

int torad(int x) { return MUL( FPI, DIV( x, I2F(180))); }
int todeg(int x) { return MUL( I2F(180), DIV( x, FPI)); }
   
int main ()
{
   int ninedegree, tmp;

   ninedegree = I2F(9);         printf("9deg_in= %f \r\n", ninedegree);
   
   tmp = SIN( torad( ninedegree));    printf("sin= %f \r\n", tmp);
   tmp = COS( torad( tmp));      printf("cos= %f \r\n", tmp);
   tmp = TAN( torad( tmp));      printf("tan= %f \r\n", tmp);
   tmp = todeg( ATN( tmp));      printf("atan= %f \r\n", tmp);
   tmp = todeg( ACS( tmp));      printf("acos= %f \r\n", tmp);
   tmp = todeg( ASN( tmp));      printf("asin= %f \r\n", tmp);
   
   printf("9deg_out= %f \r\n", tmp);
   return 0;
}

Code:
# cc -o 9d TRIG9.c -lm
ld: /lib/libm.a: out of date (warning)
# ./9d
9deg_in= 9.000000
sin= 0.156434
cos= 0.999996
tan= 0.017455
atan= 0.999996
acos= 0.157019
asin= 9.033922
9deg_out= 9.033922
#

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Sun Sep 27, 2015 3:14 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
With rearranging:
Code:
int torad(int x) { return DIV( MUL( x, FPI), I2F(180)); }
int todeg(int x) { return DIV( MUL( x, I2F(180)), FPI); }

even worse:
Code:
# ./9d
9deg_in= 9.000000
sin= 0.156434
cos= 0.999996
tan= 0.017455
atan= 0.999996
acos= 0.158262
asin= 9.106050
9deg_out= 9.106050
#

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Sun Sep 27, 2015 5:18 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
For comparison the same test compiled with PellesC under win7 64b:

Code:
FLOAT:
C:\Users\pito>C:\Users\pito\MyCode\C\9degreeok\9degreetestok\9degreetestok.exe
9deg_in= 9.000000
sin= 0.156434
cos= 0.999996
tan= 0.017455
atan= 0.999996
acos= 0.157018
asin= 9.033824
9deg_out= 9.033824

DOUBLE:
C:\Users\pito>C:\Users\pito\MyCode\C\9degreeok\9degreetestok\9degreetestok.exe
9deg_in= 9.000000
sin= 0.156434
cos= 0.999996
tan= 0.017455
atan= 0.999996
acos= 0.156434
asin= 9.000000
9deg_out= 9.000000

C:\Users\pito>

Not such as bad as I thought :)

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Mon Sep 28, 2015 6:16 am 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
With 384 (libm installed) the same results:
Code:
# cc -o 9d TRIG9.c -lm
# ./9d
9deg_in= 9.000000
sin= 0.156434
cos= 0.999996
tan= 0.017455
atan= 0.999996
acos= 0.157019
asin= 9.033922
9deg_out= 9.033922
#

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Mon Sep 28, 2015 7:10 pm 
Committer
User avatar

Joined: Wed Oct 10, 2012 11:01 pm
Posts: 1079
Location: Sunnyvale, CA
So we have some basic support for floats - good.
Hopefully Alex will eventually implement it in the SmallerC compiler.


Top
 Profile  
 
PostPosted: Wed Sep 30, 2015 6:30 am 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
And printf with formatted floats works too (v390) :)
Code:
        ninedegree = I2F(9);                    printf("9deg_in= %1.7e \r\n", n$

        tmp = SIN( torad( ninedegree));         printf("sin= %1.7e \r\n", tmp);
        tmp = COS( torad( tmp));                printf("cos= %1.7e \r\n", tmp);
        tmp = TAN( torad( tmp));                printf("tan= %1.7e \r\n", tmp);
        tmp = todeg( ATN( tmp));                printf("atan= %1.7e \r\n", tmp);
        tmp = todeg( ACS( tmp));                printf("acos= %1.7e \r\n", tmp);
        tmp = todeg( ASN( tmp));                printf("asin= %1.7e \r\n", tmp);

        printf("9deg_out= %1.7e \r\n", tmp);

-----emg: TRIG9.c                         (fundamental)--L63--C67---------------
# cc -o 9d TRIG9.c -lm
# ./9d
9deg_in= 9.0000000e+00
sin= 1.5643450e-01
cos= 9.9999630e-01
tan= 1.7455000e-02
atan= 9.9999630e-01
acos= 1.5701920e-01
asin= 9.0339220e+00
9deg_out= 9.0339220e+00
#

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Sun Jan 17, 2016 8:32 am 
Contributor

Joined: Mon Apr 29, 2013 1:56 am
Posts: 196
As of revision 482 you should be able to do things like this with Smaller C:

Code:
# cat fptst.c
#include <stdio.h>
#include <math.h>

#define PI 3.1415927f

int main(void)
{
  float f;
  float nan = 0./0;
  float inf = 1./0;
  unsigned u;
  for (f = 0; f < 1; f = f + 0.1)
    printf("%f\n", f);
  printf("%f %f %f\n", -inf, nan, +inf);
  printf("%d %d %d %d\n", nan < 0, nan == 0, nan > 0, nan != 0);
  printf("%f\n", sqrt(65536.));
  printf("%f\n", sin(PI/4));
  u = -1u; f = 12345678;
  printf("%f %u\n", (float)u, (unsigned)f);
  f = 9*PI/180;
  f = tan(cos(sin(f)));
  f = asin(acos(atan(f)));
  f = f*180/PI;
  printf("%f\n", f);
  return 0;
}


Code:
# cc fptst.c -lm -o fptst
# ./fptst
0.000000
0.100000
0.200000
0.300000
0.400000
0.500000
0.600000
0.700000
0.800000
0.900000
-Inf NaN Inf
0 0 0 1
256.000000
0.707107
4294966000.000000 12345678
8.999962


Top
 Profile  
 
PostPosted: Sun Jan 17, 2016 6:38 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Code:
  f = 9*PI/180;
  f = tan(cos(sin(f)));
  f = asin(acos(atan(f)));
  f = f*180/PI;
  printf("%f\n", f);

That is not good.. Pls have a look at my code there.. You have to convert torad/todeg between the trig calcs..

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Sun Jan 17, 2016 9:35 pm 
Contributor

Joined: Mon Apr 29, 2013 1:56 am
Posts: 196
You're right, I missed the other conversions.

Anyhow, the point was that you don't need hacks with direct invocation of things like __mulsf3 anymore.


Top
 Profile  
 
PostPosted: Mon Jan 18, 2016 7:19 am 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Great, I will try during the weekend..

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
PostPosted: Mon Jan 18, 2016 3:46 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Btw, this is a single/double comparison of the test (PellesC):
Code:
C:\Users\pito>C:\Users\pito\MyCode\C\9degreeok\9degreetestok\9degreetestok.exe
9deg_in= 9.000000000000000e+00
sin= 1.564344650402309e-01
cos= 9.999962727428850e-01
tan= 1.745499985548866e-02
atan= 9.999962727428850e-01
acos= 1.564344650396744e-01
asin= 8.999999999967719e+00
9deg_out= 8.999999999967719e+00

C:\Users\pito>

C:\Users\pito>C:\Users\pito\MyCode\C\9degreeok\9degreetestok\9degreetestok.exe
9deg_in= 9.0000000e+00
sin= 1.5643448e-01
cos= 9.9999624e-01
tan= 1.7455000e-02
atan= 9.9999624e-01
acos= 1.5701751e-01
asin= 9.0338240e+00
9deg_out= 9.0338240e+00

C:\Users\pito>

The results depend on the quality of the math functions implementation, however.
See below the results for various famous calculators. Mind the binary coded floating point usage is rare with calculators.
http://www.rskey.org/~mwsebastian/miscprj/results.htm

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron




Powered by phpBB® Forum Software © phpBB Group

BSD Daemon used with permission