C , . , ++. , (+, -, *,/,%, + =, - =, * =,/=,% =, -, ++, ), .
C , . .
1: .
, , . , , , typedef 'd , . :
int64_t a_7 = (int64_t)(7.3*(1<<7));
int64_t b_5 = (int64_t)(3.78*(1<<5));
int64_t sum_7 = a_7 + (b_5 << 2);
int64_t product_12 = a_7 * b_5;
, , , , .
2: . :
typedef struct FixedPoint {
int64_t data;
uint8_t pointPosition;
} FixedPoint;
FixedPoint fixed_add(FixedPoint a, FixedPoint b) {
if(a.pointPosition >= b.PointPosition) {
return (FixedPoint){
.data = a.data + (b.data << a.pointPosition - b.pointPosition),
.pointPosition = a.pointPosition
};
} else {
return (FixedPoint){
.data = (a.data << b.pointPosition - a.pointPosition) + b.data,
.pointPosition = b.pointPosition
};
}
}
, . :
.
Copy structures to pass parameters and results, or markup the pointer if you use pointers.
The need to calculate point corrections at runtime.
This is very similar to the overhead of a C ++ class without templates. Using templates will take some decisions back to compilation time by reducing flexibility.
This object-oriented approach is probably the most flexible, and it allows you to transparently add support for non-binary dot positions.
source
share