c - Why can these be declared as integers? -


in program seems work have following declarations:

#include <stdio.h> #include "system.h"  signed char input[4]; /* 4 recent input values */  char get_q7( void ); void put_q7( char ); void firfixed(signed char input[4]);  const int c0 = (0.0299 * 128 + 0.5); /* converting float q7 multiplying 2^n i.e. 128 = 2^7 since use q7 , round nearest integer*/ const int c1 = (0.4701 * 128 + 0.5); const int c2 = (0.4701 * 128 + 0.5); const int c3 = (0.0299 * 128 + 0.5); const int half = (0.5000 * 128 + 0.5);  enum { q7_bits = 7 };  void firfixed(signed char input[4]) {     int sum = c0*input[0] + c1*input[1] + c2*input[2] + c3*input[3];     signed char output = (signed char)((sum + half) >> q7_bits);     put_q7(output); }  int main(void) {        int a;     while(1)     {          (a = 3 ; > 0 ; a--)     {       input[a] = input[a-1];     }            input[0]=get_q7();                 firfixed(input);     }     return 0; } 

but don't understand how it's possibl declare fractional number int done constants. it's supposed q7 notation why done way , how can compiler accept int fractional number? take integer part of fraction , if why isn't cast required?

when floating point value converted integer, it's truncated. means digits after decimal point removed. e.g. 12.34 becomes 12, , 12.99 also becomes 12. there no rounding done.

that + 0.5 part in initializations. it's way convert floating point value integer rounding. example, 0.0299 * 128 3.8272 truncated 3. + 0.5 it's 4.3272 truncated becomes 4, rounded value of 3.8272.

there may still problems, , might want read what every computer scientist should know floating-point arithmetic learn more.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -