Loading lang_c_tr_01...
void hello(void);
<stdio.h>doit être inclus pour rendre accessible la fonction standard printf().
<stdbool.h>.
<stdint.h>.
const double pi=3.14159265358979323846;
//----------------------------------------------------------------------------
#ifndef MYUTILS_H #define MYUTILS_H
#include <stdbool.h> #include <stdint.h>
void hello(void);
bool withinBounds(double x, double low, double high);
bool outOfBounds(double x, double low, double high);
int maxOfThreeIntegers_v1(int a, int b, int c);
int maxOfThreeIntegers_v2(int a, int b, int c);
double minOfThreeReals_v1(double a, double b, double c);
double minOfThreeReals_v2(double a, double b, double c);
int integerCeil(double x);
int integerRound(double x);
double integerPower(double x, int p);
int powerOfTwo(double x);
uint16_t saturateTwice(uint16_t value);
uint16_t saturateSquare(uint16_t value);
double computeSqrt(double x);
double computeSin(double x);
#endif // MYUTILS_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "myUtils.h" #include <stdio.h>
void hello(void) { printf("\n~~~~ hello() ~~~~\n"); }
bool withinBounds(double x, double low, double high) { return (x>=low)&&(x<=high); }
bool outOfBounds(double x, double low, double high) { return (x<low)||(x>high); }
int maxOfThreeIntegers_v1(int a, int b, int c) { int result; if(a>b) { if(a>c) { result=a; } else { result=c; } } else { if(b>c) { result=b; } else { result=c; } } return result; }
int maxOfThreeIntegers_v2(int a, int b, int c) { return a>b ? ( a>c ? a : c ) : ( b>c ? b : c ); }
double minOfThreeReals_v1(double a, double b, double c) { double result; if(a<b) { if(a<c) { result=a; } else { result=c; } } else { if(b<c) { result=b; } else { result=c; } } return result; }
double minOfThreeReals_v2(double a, double b, double c) { return a<b ? ( a<c ? a : c ) : ( b<c ? b : c ); }
static // internal use int positiveIntegerCeil(double x) { int floor=(int)x; return floor==x ? floor : floor+1; }
int integerCeil(double x) { return x>=0.0 ? positiveIntegerCeil(x) : -positiveIntegerCeil(-x); }
static // internal use int positiveIntegerRound(double x) { int floor=(int)x; int ceil=floor+1; return (x-floor)<(ceil-x) ? floor : ceil; }
int integerRound(double x) { return x>=0.0 ? positiveIntegerRound(x) : -positiveIntegerRound(-x); }
static // internal use double positiveIntegerPower(double x, int p) { double result=1.0; for(int i=0; i<p; ++i) { result*=x; } return result; }
double integerPower(double x, int p) { return p>=0 ? positiveIntegerPower(x, p) : 1.0/positiveIntegerPower(x, -p); }
static // internal use int positivePowerOfTwo(double x) { int power=0; for(int i=1; i<x; i*=2) { ++power; } return power; }
int powerOfTwo(double x) { if(x>=1.0) { return positivePowerOfTwo(x); } if(x>0.0) { return -positivePowerOfTwo(1.0/x); } return 0; }
uint16_t saturateTwice(uint16_t value) { uint32_t wideValue=value; uint32_t wideResult=2*wideValue; uint16_t result=(uint16_t)wideResult; return result==wideResult ? result : UINT16_MAX; }
uint16_t saturateSquare(uint16_t value) { uint32_t wideValue=value; uint32_t wideResult=wideValue*wideValue; uint16_t result=(uint16_t)wideResult; return result==wideResult ? result : UINT16_MAX; }
double computeSqrt(double x) { if(x<=0.0) { return 0.0; } double y=x; for( ; ; ) { double prev=y; y=0.5*(y+x/y); if(prev==y) { break; } } return y; }
double computeSin(double x) { const double pi=3.14159265358979323846; while(x<-pi) { x+=2.0*pi; } while(x>pi) { x-=2.0*pi; } double x2=x*x; double numerator=x; double denominator=1.0; double y=numerator/denominator; for(int k=3; ; k+=2) { double prev=y; numerator*=-x2; denominator*=(k-1.0)*k; y+=numerator/denominator; if(prev==y) { break; } } return y; }
//----------------------------------------------------------------------------
/*---------------------------------------------------------------------------- Question 1.1 ~~~~~~~~~~~~
Consommation énergétique totale initiale : 20 % + 80 % = 100 % Consommation énergétique totale de la version optimisée : 20 % + 2/3 x 80 % ≈ 73 % L'autonomie est multipliée par : 100 % / 73 % ≈ 1.36
L'effort d'optimisation apporte un gain en autonomie significatif dans le cas présent. Sur l'exemple hypothétique d'un téléphone ayant 18 h d'autonomie, nous dépasserions les 24 h. Sur celui de l'ordinateur portable qui visionne un film, le fait de passer d'1 h 30 d'autonomie à plus de 2 h est appréciable.
------------------------------------------------------------------------------ Question 1.2 ~~~~~~~~~~~~
Consommation moyenne sur l'année 2014 : 70x10^12 W.h / (365x24 h) ≈ 8.2x10^9 W Contribution d'un réacteur nucléaire à cette consommation : 1x10^9 W / 8.2x10^9 W ≈ 12 %
Diminuer de seulement 12 % la durée d'un traitement qui n'a pas encore été optimisé est tout à fait envisageable.
----------------------------------------------------------------------------*/
#include <stdio.h> #include "myUtils.h"
void test_withinBounds(void) { printf("\n~~~~ test_withinBounds() ~~~~\n"); bool r1=withinBounds(2.2, 5.5, 8.8); bool r2=withinBounds(5.5, 2.2, 8.8); bool r3=withinBounds(8.8, 2.2, 5.5); printf("%d %d %d\n", r1, r2, r3); }
void test_outOfBounds(void) { printf("\n~~~~ test_outOfBounds() ~~~~\n"); bool r1=outOfBounds(2.2, 5.5, 8.8); bool r2=outOfBounds(5.5, 2.2, 8.8); bool r3=outOfBounds(8.8, 2.2, 5.5); printf("%d %d %d\n", r1, r2, r3); }
void test_maxOfThreeIntegers_v1(void) { printf("\n~~~~ test_maxOfThreeIntegers_v1() ~~~~\n"); int r1=maxOfThreeIntegers_v1(20, 12, 5); int r2=maxOfThreeIntegers_v1(12, 20, 5); int r3=maxOfThreeIntegers_v1(20, 5, 12); printf("%d %d %d\n", r1, r2, r3); }
void test_maxOfThreeIntegers_v2(void) { printf("\n~~~~ test_maxOfThreeIntegers_v2() ~~~~\n"); int r1=maxOfThreeIntegers_v2(20, 12, 5); int r2=maxOfThreeIntegers_v2(12, 20, 5); int r3=maxOfThreeIntegers_v2(20, 5, 12); printf("%d %d %d\n", r1, r2, r3); }
void test_minOfThreeReals_v1(void) { printf("\n~~~~ test_minOfThreeReals_v1() ~~~~\n"); double r1=minOfThreeReals_v1(5, 12, 20); double r2=minOfThreeReals_v1(12, 5, 20); double r3=minOfThreeReals_v1(20, 12, 5); printf("%g %g %g\n", r1, r2, r3); }
void test_minOfThreeReals_v2(void) { printf("\n~~~~ test_minOfThreeReals_v2() ~~~~\n"); double r1=minOfThreeReals_v2(5, 12, 20); double r2=minOfThreeReals_v2(12, 5, 20); double r3=minOfThreeReals_v2(20, 12, 5); printf("%g %g %g\n", r1, r2, r3); }
void test_integerCeil(void) { printf("\n~~~~ test_integerCeil() ~~~~\n"); for(double x=-2.0; x<=2.0; x+=0.125) { int r=integerCeil(x); printf("%g\t-->\t%d\n", x, r); } /*** (gdb) backtrace #0 positiveIntegerCeil (x=2) at module01.c:112 #1 0x0000555555557c75 in integerCeil (x=-2) at module01.c:120 #2 0x0000555555557554 in test_integerCeil () at prog01.c:72 #3 0x00005555555579b7 in main () at prog01.c:192 ***/ }
void test_integerRound(void) { printf("\n~~~~ test_integerRound() ~~~~\n"); for(double x=-2.0; x<=2.0; x+=0.125) { int r=integerRound(x); printf("%g\t-->\t%d\n", x, r); } /*** (gdb) backtrace #0 positiveIntegerRound (x=2) at module01.c:127 #1 0x0000555555557d45 in integerRound (x=-2) at module01.c:136 #2 0x00005555555575c7 in test_integerRound () at prog01.c:90 #3 0x00005555555579bc in main () at prog01.c:193 ***/ }
void test_integerPower(void) { printf("\n~~~~ test_integerPower() ~~~~\n"); for(double x=-1.5; x<=1.5; x+=1.0) { for(int p=-3; p<=3; ++p) { double r=integerPower(x, p); printf("(%g)^%d \t-->\t%g\n", x, p, r); } } }
void test_powerOfTwo(void) { printf("\n~~~~ test_powerOfTwo() ~~~~\n"); for(double x=0.0625; x<=16.0; x*=1.2599210498948732) { int r=powerOfTwo(x); printf("%g \t-->\t%d\n", x, r); } printf("0.0 \t-->\t%d\n", powerOfTwo(0.0)); printf("-0.5 \t-->\t%d\n", powerOfTwo(-0.5)); }
void test_saturateTwice(void) { printf("\n~~~~ test_saturateTwice() ~~~~\n"); for(uint16_t value=0; ; ++value) { uint16_t result=saturateTwice(value); if(result==UINT16_MAX) { printf("%d \t-->\t%d\n", value, result); break; } } }
void test_saturateSquare(void) { printf("\n~~~~ test_saturateSquare() ~~~~\n"); for(uint16_t value=0; ; ++value) { uint16_t result=saturateSquare(value); if(result==UINT16_MAX) { printf("%d \t-->\t%d\n", value, result); break; } } }
void test_computeSqrt(void) { printf("\n~~~~ test_computeSqrt() ~~~~\n"); for(double x=0.0; x<=16.0; x+=0.5) { double y=computeSqrt(x); printf("%g \t-->\t%g\n", x, y); } }
void test_computeSin(void) { printf("\n~~~~ test_computeSin() ~~~~\n"); const double pi=3.14159265358979323846; for(double x=-pi; x<=pi; x+=pi/6.0) { double y=computeSin(x); printf("%g \t-->\t%g\n", x, y); } }
int main(void) { hello(); test_withinBounds(); test_outOfBounds(); test_maxOfThreeIntegers_v1(); test_maxOfThreeIntegers_v2(); test_minOfThreeReals_v1(); test_minOfThreeReals_v2(); test_integerCeil(); test_integerRound(); test_integerPower(); test_powerOfTwo(); test_saturateTwice(); test_saturateSquare(); test_computeSqrt(); test_computeSin(); return 0; }
//----------------------------------------------------------------------------