1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <nds.h>
#include <stdio.h>
#define Fixed19_12tofloat(i) (((float)(i)) / (float)(1<<(12)))
#define floattoFixed19_12(i) ((int)((i) * (float)(1<<(12))))
typedef int fixed19_12; // 1 signe, 19 partie entière, 12partie décimale
// fonction posant problème
// le calcul en morceaux est nécessaire pour ne pas tronquer la partie entière
fixed19_12 multf19_12(fixed19_12 n1, fixed19_12 n2) {
int i1,i2,f1,f2;
f1=(n1&0x0FFF); // récupération des parties décimales
f2=(n2&0x0FFF);
i1=((n1&0xFFFFF000)>>12); // récupération des parties entières
i2=((n2&0xFFFFF000)>>12);
return (((f1*f2)>>12) + (f1*i2) + (i1*f2) + ((i1*i2)<<12));
}
int main() {
fixed19_12 a = floattoFixed19_12(0.625f);
fixed19_12 b = floattoFixed19_12(-7.0625f);
// calculs tests
printf("a: %f b: %f\n", Fixed19_12tofloat(a), Fixed19_12tofloat(b));
printf("a+b: %f\n", Fixed19_12tofloat(a+b));
printf("a-b: %f\n", Fixed19_12tofloat(a-b));
printf("ax(-b): %f\n", Fixed19_12tofloat(multf19_12(a,-b)));
printf("axb: %f\n", Fixed19_12tofloat(multf19_12(a,b)));
/* Résultats:
a: 0,625000 b: -7.062500
a+b: -6.437500
ax(-b): 4.414062
axb: -393220.406250
*/
} |
Partager