关于pow函数
一个判断数字黑洞的程序
#include<stdio.h>
#include<math.h>
int function(int n)
{
if(n==153) printf("满足153黑洞!\n");
else
{
int x1,x2,x3,x4,m;
x1=n/1000;
x2=n%1000/100;
x3=n%100/10;
x4=n%10;
m=pow(x1,3)+pow(x2,3)+pow(x3,3)+pow(x4,3);
/*double y1,y2,y3,y4;
y1=pow(x1,3);
y2=pow(x2,3);
y3=pow(x3,3);
y4=pow(x4,3);
m=int(y1+y2+y3+y4);*/
/*m=x1*x1*x1+x2*x2*x2+x3*x3*x3+x4*x4*x4;*/
function(m);
}
}
int main()
{
int num[972];
for(int i=0;i<972;i++)
num[i]=3*(i+1);
for(int i=0;i<972;i++)
{printf("%d:",num[i]);function(num[i]);}
}
运行后判断到6出错0xc00000fd,
m=pow(x1,3)+pow(x2,3)+pow(x3,3)+pow(x4,3);把这句改成注释那两种就不会出错
请教原因。
出现上面的原因也没什么稀奇,根据pow的定义,由于在类型转换时有隐式取整,导致三种方式的结果可能不一致。
“C++提供以下几种pow函数的重载形式:
double pow(double X,int Y);
float pow(float X,float Y);
float pow(float X,int Y);
long double pow(long double X,long double Y);
long double pow(log double X,int Y);
使用的时候应合理设置参数类型,避免有多”
[解决办法]
常量也有类型!
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
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 );