user-0 提问: 请教printf除法遇到的问题

回复:
user: user-0
content: FUCK! 问题找到。  typedef struct {    int16   degC;    int16  mV; } adc_degC_to_mV_map_type;

user: user-0
content: 引用 7 楼 zilaishuichina 的回复:那我觉得  你可以试一下    printf("d:%d\r\n", d = a * b) ; printf("res:%d\r\n", d / c) ;  测试过了： d:-460 res: 0

user: user-1
content: 那我觉得  你可以试一下    printf("d:%d\r\n", d = a * b) ; printf("res:%d\r\n", d / c) ;

user: user-0
content: 引用 5 楼 zilaishuichina 的回复:[quote=引用 4 楼 dsoyy 的回复:] [quote=引用 3 楼 zilaishuichina 的回复:] 编译器 先算了 (-92)/(-94)   ， 再算的 5*0  ，结果等于0   printf("res:%d\r\n",( ((a)*(b))/(c) )) ;  结果还是0，因此你的推断不正确[/quote]  vs下面测试没有问题，结果为4  建议你在printf("res:%d\r\n",( a*b/c )) ;这一行下数据断点，看看是不是a,b,c中的某一个值 ， 或某几个值改变了  建议发出完整代码，因为有可能是多线程的原因导致的变量值发生变化，或者是printf("res:%d\r\n",( a*b/c )) ;这一行之前有内存写越界导致的变量值发生变化。[/quote]  我怀疑是WInTC这个编译环境，我没办法用断点，所以是打印出来的。  下面贴完整代码： #define TRUE 1 #define FALSE 0  typedef unsigned char  uint8; typedef unsigned char boolean; typedef unsigned long int uint32; typedef signed   short int16; typedef unsigned short uint16; typedef signed long int int32;  typedef struct {    int16   degC;    uint16  mV; } adc_degC_to_mV_map_type;  /*该参数表需要根据使用NTC调整*/ static const adc_degC_to_mV_map_type adc_xo_therm_map[] = {    { -45, 2160 },       { -40, 2148 },    { -35, 2132 },     { -30, 2112 },    { -25, 2088 },     { -20, 2058 },    { -15, 2023 },    { -10, 1981 },    { -5, 1932 },    {  0, 1876 },    {  5,  1813 },    {  10, 1743 },    {  15, 1666 },    {  20, 1584 },    {  25, 1497 },    {  30, 1406  },    {  35, 1312  },    {  40, 1218  },    {  45, 1125  },    {  50, 1033 },    {  55,  944 },    {  60,  860 },    {  65,  781 },    {  70,  708 },    {  75,  639 },    {  80,  577 },    {  85,  519 },    {  90,  467 } }; /*计算结果，返回值为整数*/    uint32 search_index=0;  static int32 adc_calc_degC_from_mV(    const adc_degC_to_mV_map_type   *table,    uint32                           tableSize,    int32                            mV ) {     boolean descending = TRUE;     int32 a,b,c,d = 0;     int32 mV2_buf = 0;     int32 mV3_buf = 0;     int32 Tem_buf = 0;     int32 degC_buf = 0;     if (table==NULL)     {        return ;     }     if((table[0].mV < mV) || (table[tableSize-1].mV > mV))     {        return ;     }     /* Check if table is descending or ascending */     if (tableSize>1)     {       if (table[0].mV < table[1].mV)       {         descending=FALSE;       }     }      while (search_index < tableSize)     {       if ( (descending==TRUE) && (table[search_index].mV < mV) )       {         /* table entry is less than measured value and table is descending, stop */         break;       }       else if ( (descending==FALSE) && (table[search_index].mV > mV) )       {         /* table entry is greater than measured value and table is ascending, stop */         break;       }       else       {         search_index++;       }     }        if (search_index==0)     {       return table[0].degC;     }     else if (search_index==tableSize)     {       return table[tableSize-1].degC;     }     else     {       /* result is between search_index and search_index-1 */            printf("a:%d\r\n",a = table[search_index].degC-table[search_index-1].degC);          printf("b:%d\r\n",b = (mV-table[search_index-1].mV));          printf("c:%d\r\n",c = table[search_index].mV-table[search_index-1].mV);              printf("res:%d\r\n",( ((a)*(b))/(c) )) ;       /* interpolate linearly */         return (                ( (int32)                    (                     ( table[search_index].degC-table[search_index-1].degC)                      *( mV-table[search_index-1].mV)                    )                    / ( table[search_index].mV-table[search_index-1].mV)                )                + table[search_index-1].degC              );      } }      main() {     uint32   temp = 0;     uint32   V_mv = 1314;      temp = adc_calc_degC_from_mV(adc_xo_therm_map,sizeof(adc_xo_therm_map)/sizeof(adc_xo_therm_map[0]),V_mv);     printf("%d",temp);     getch();  }

user: user-1
content: 引用 4 楼 dsoyy 的回复:[quote=引用 3 楼 zilaishuichina 的回复:] 编译器 先算了 (-92)/(-94)   ， 再算的 5*0  ，结果等于0   printf("res:%d\r\n",( ((a)*(b))/(c) )) ;  结果还是0，因此你的推断不正确[/quote]  vs下面测试没有问题，结果为4  建议你在printf("res:%d\r\n",( a*b/c )) ;这一行下数据断点，看看是不是a,b,c中的某一个值 ， 或某几个值改变了  建议发出完整代码，因为有可能是多线程的原因导致的变量值发生变化，或者是printf("res:%d\r\n",( a*b/c )) ;这一行之前有内存写越界导致的变量值发生变化。

user: user-0
content: 引用 3 楼 zilaishuichina 的回复:编译器 先算了 (-92)/(-94)   ， 再算的 5*0  ，结果等于0   printf("res:%d\r\n",( ((a)*(b))/(c) )) ;  结果还是0，因此你的推断不正确

user: user-1
content: 编译器 先算了 (-92)/(-94)   ， 再算的 5*0  ，结果等于0

user: user-2
content: C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.  Syntax  floating-constant :  fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt  fractional-constant :  digit-sequenceopt . digit-sequence digit-sequence .  exponent-part :  e signopt digit-sequence E signopt digit-sequence  sign : one of  + –  digit-sequence :  digit digit-sequence digit  floating-suffix :one of  f l F L  Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:  18.46 38.  The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:  18.46e0      // 18.46 18.46e1      // 184.6  If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.  Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.  Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like  void func( double );  and   void func( long double );

user: user-2
content: 请牢记：常量也有类型！  C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.  Syntax  integer-constant :  decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence'  decimal-constant :  nonzero-digit decimal-constant digit  octal-constant :  0 octal-constant octal-digit  hexadecimal-constant :  0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit  nonzero-digit : one of  1 2 3 4 5 6 7 8 9  octal-digit : one of  0 1 2 3 4 5 6 7  hexadecimal-digit : one of  0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F  integer-suffix :  unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt  unsigned-suffix : one of  u U  long-suffix : one of  l L  64-bit integer-suffix :  i64  To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.  To specify a decimal constant, begin the specification with a nonzero digit. For example:  int i = 157;   // Decimal constant int j = 0198;  // Not a decimal number; erroneous octal constant int k = 0365;  // Leading zero specifies octal constant, not decimal  To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:  int i = 0377;   // Octal constant int j = 0397;   // Error: 9 is not an octal digit  To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:  int i = 0x3fff;   // Hexadecimal constant int j = 0X3FFF;   // Equal to i  To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:  unsigned uVal = 328u;             // Unsigned value long lVal = 0x7FFFFFL;            // Long value specified                                    //  as hex constant unsigned long ulVal = 0776745ul;  // Unsigned long value