Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:52 07 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : uM2(+): V5.2 strings, maths & CFunctions

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 12:14am 24 Jun 2016
Copy link to clipboard 
Print this post

V5.2 includes the facility for typed CFunctions so for the first time a CFunction can return a floating point number or a string. This allows the possibility of doing complex maths or string manipulation much faster than is possible in Basic. Obviously the biggest advantage comes with things that need an iterative type approach.

In this thread I will post simple specific CFunctions as and when I write them. If there is a specific function you need which would benefit from the faster processing in C post the algorithm and although I make no promises I'll try and convert it.

First off atan2

Although this isn't a very complex algorithm I have tested the Cfunction and it operates 3x faster than the equivalent Basic version. When stored in the library the code only takes 177 words in memory.

? atan2(1,-1)
end

Cfunction atan2(float, float) float
00000000
27BDFFC8 AFBF0034 AFBE0030 AFB7002C AFB60028 AFB50024 AFB20018 AFB10014
AFB00010 AFB40020 3C109D00 AFB3001C 8E02009C 8C930000 3C044049 24840FDA
0040F809 8CB40000 0040F021 8E02009C 3C043FC9 0040F809 24840FDA 0040A821
8E02009C 3C04BFC9 0040F809 24840FDA 0040B821 8E02009C 3C0440C9 0040F809
24840FDA 0040B021 8E02009C 0040F809 00002021 00408821 8E02009C 0040F809
00002021 00409021 8E020068 02802021 0040F809 00002821 24030001 1443000B
3C029D00 3C039D00 8C620064 8C700078 02802821 0040F809 02602021 0200F809
00402021 10000056 00409021 8C420068 02602021 0040F809 02202821 2403FFFF
10430016 3C029D00 8C420068 02802021 0040F809 02202821 2403FFFF 1443000F
3C029D00 3C039D00 8C620064 8C720078 02802821 02602021 0040F809 8C70005C
0240F809 00402021 00402821 0200F809 03C02021 1000003A 00409021 8C420068
02602021 0040F809 02202821 2403FFFF 14430016 3C029D00 8C420068 02802021
0040F809 02202821 2403FFFF 1443000F 3C029D00 3C039D00 8C620064 8C720078
02802821 02602021 0040F809 8C700060 0240F809 00402021 00402021 0200F809
03C02821 1000001E 00409021 8C420068 02602021 0040F809 02202821 24030001
14430008 3C029D00 8C420068 02802021 0040F809 02202821 50400011 02A09021
3C029D00 8C420068 02602021 0040F809 02202821 2403FFFF 1443000A 3C029D00
8C420068 02802021 0040F809 02202821 50400003 02E09021 10000002 3C029D00
3C029D00 8C420068 02402021 0040F809 02202821 2403FFFF 14430008 8FBF0034
3C029D00 8C42005C 02402021 0040F809 02C02821 00409021 8FBF0034 02401021
8FBE0030 8FB7002C 8FB60028 8FB50024 8FB40020 8FB3001C 8FB20018 8FB10014
8FB00010 03E00008 27BD0038
End CFunction


C source

float Atan2(float *yy,float *xx){
float x=*xx,y=*yy;
float PI = LoadFloat(0x40490FDA);
float PIby2 = LoadFloat(0x3FC90FDA);
float PIbyneg2 = LoadFloat(0xBFC90FDA);
float twoPI = LoadFloat(0x40C90FDA);
float R_0=LoadFloat(0x00000000);
float atan2ret=LoadFloat(0x00000000);
if (FCmp(x , 0)==1 ){
atan2ret = atanf(FDiv(y,x));
}
else if( (FCmp(y , R_0)!=-1) && (FCmp(x , R_0)==-1)){
atan2ret = FAdd(PI , atanf(FDiv(y,x))) ;
}
else if((FCmp(y , R_0)==-1) && (FCmp(x , R_0)==-1)){
atan2ret = FSub(atanf(FDiv(y,x)) , PI) ;
}
else if ((FCmp(y , R_0)==1) && (FCmp(x , R_0)==0) ){
atan2ret = PIby2 ;
}
else if ( (FCmp(y , R_0)==-1) && (FCmp(x , R_0)==0)){
atan2ret = PIbyneg2 ;
}
if ( FCmp(atan2ret , R_0)==-1) {
atan2ret = FAdd(atan2ret , twoPI );
}
return atan2ret;
}
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 03:22am 24 Jun 2016
Copy link to clipboard 
Print this post

The most obvious one would be sorting an array,
Maybe a non recursive quick sort?

Microblocks. Build with logic.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 03:27am 24 Jun 2016
Copy link to clipboard 
Print this post

There are a full set of sorts in the 5.2 firmware download together with other CFunction goodies. Look in the "Embedded C Modules" directory
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 04:26pm 24 Jun 2016
Copy link to clipboard 
Print this post

What data types can be used ? If a float is 32-bits it implies
any other 32 bit type would fit , does MMBasic incorporate them ?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10215
Posted: 09:44pm 24 Jun 2016
Copy link to clipboard 
Print this post

  Quote  What data types can be used ? If a float is 32-bits it implies
any other 32 bit type would fit , does MMBasic incorporate them ?


The Micromite supports 64-bit integers, 32 byte floats and strings up to 255 bytes. Anything else can be used in C but to be usable in Basic the function return must be one of the three standard datatypes
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1114
Posted: 06:04pm 25 Jun 2016
Copy link to clipboard 
Print this post

The 32 byte floats will surely offer more precision than anyone could ever want? hee, hee, hee.
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
piclover
Senior Member

Joined: 14/06/2015
Location: France
Posts: 134
Posted: 02:27am 28 Jun 2016
Copy link to clipboard 
Print this post

  panky said   The 32 byte floats will surely offer more precision than anyone could ever want? hee, hee, hee.
Not really... 32 bits floating point numbers are known as "single precision" floating point values.

Double precision (64 bits) is required for accurate astronomical calculations for instance (such as Moon rise/set), if you want to attain an accuracy of a few seconds in the astronomical event, and yes, I did use such astronomical calculations in a Micromite project already: my meteo station also gives the sunset, meridian, sunrise, moon age, moon phase (in percent of the lighted surface).

Double precision is also needed in any complex calculations where the accumulated error of each mathematical computation quickly (and sometimes exponentially) accumulates with the error of the former calculation.

Alas, while "long float" variables are usable with PIC32 chips, you can't use them in a CRoutine, since they make use of the floating point library and MM.BASIC won't be able to deal with a 64 bits floating point result anyway: 64 bits floats is perhaps something to consider for implementation in the MX470 version ?Edited by piclover 2016-06-29
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 03:27am 28 Jun 2016
Copy link to clipboard 
Print this post

Read carefully. :)
he said '32 BYTE'.
Microblocks. Build with logic.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025