#305: 請問這是啥意思?


xup6m3fu0 (dillon)


編譯錯誤, 請檢查語法是否符合系統(ANSI/ISO C/C++ by GNU)的要求。
錯誤訊息:
/tmp/code_29457.cpp: In function `int main()':
/tmp/code_29457.cpp:7: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/c++/3.3/cmath:550: error:                 long double
   std::sqrt(long double)
/usr/include/c++/3.3/cmath:546: error:                 float std::sqrt(float)
#309: Re:請問這是啥意思?


shik (shik)


編譯錯誤, 請檢查語法是否符合系統(ANSI/ISO C/C++ by GNU)的要求。
錯誤訊息:
/tmp/code_29457.cpp: In function `int main()':
/tmp/code_29457.cpp:7: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/c++/3.3/cmath:550: error:                 long double
   std::sqrt(long double)
/usr/include/c++/3.3/cmath:546: error:                 float std::sqrt(float)
 
 



ZeroJudge的Compiler使用sqrt函式時引數要是double型態..

 ex:

int n = 100;

cout << sqrt( n*1.0 ); 

#312: Re:請問這是啥意思?


POOHccc ()


編譯錯誤, 請檢查語法是否符合系統(ANSI/ISO C/C++ by GNU)的要求。
錯誤訊息:
/tmp/code_29457.cpp: In function `int main()':
/tmp/code_29457.cpp:7: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/c++/3.3/cmath:550: error:                 long double
   std::sqrt(long double)
/usr/include/c++/3.3/cmath:546: error:                 float std::sqrt(float)
 
 



ZeroJudge的Compiler使用sqrt函式時引數要是double型態..

 ex:

int n = 100;

cout << sqrt( n*1.0 ); 



cout << sqrt( n*1.0 );

                    ^^ 這樣n還是int資料型態

 

有二種方法:

  1. cout << sqrt((double)n);
  2. cout << sqrt(1.0 * n);     ※注意: sqrt(1.0 * n) 和 sqrt( n*1.0 )不一樣,一個是強制資料型態為double、一個是int
#314: Re:請問這是啥意思?


shik (shik)


編譯錯誤, 請檢查語法是否符合系統(ANSI/ISO C/C++ by GNU)的要求。
錯誤訊息:
/tmp/code_29457.cpp: In function `int main()':
/tmp/code_29457.cpp:7: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/c++/3.3/cmath:550: error:                 long double
   std::sqrt(long double)
/usr/include/c++/3.3/cmath:546: error:                 float std::sqrt(float)
 
 



ZeroJudge的Compiler使用sqrt函式時引數要是double型態..

 ex:

int n = 100;

cout << sqrt( n*1.0 ); 



cout << sqrt( n*1.0 );

                    ^^ 這樣n還是int資料型態

 

有二種方法:

  1. cout << sqrt((double)n);
  2. cout << sqrt(1.0 * n);     ※注意: sqrt(1.0 * n) 和 sqrt( n*1.0 )不一樣,一個是強制資料型態為double、一個是int

不甚了解,可否煩請解釋為何兩者會有差別?

 

#315: Re:請問這是啥意思?


POOHccc ()


編譯錯誤, 請檢查語法是否符合系統(ANSI/ISO C/C++ by GNU)的要求。
錯誤訊息:
/tmp/code_29457.cpp: In function `int main()':
/tmp/code_29457.cpp:7: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157: error: candidates are: double sqrt(double)
/usr/include/c++/3.3/cmath:550: error:                 long double
   std::sqrt(long double)
/usr/include/c++/3.3/cmath:546: error:                 float std::sqrt(float)
 
 



ZeroJudge的Compiler使用sqrt函式時引數要是double型態..

 ex:

int n = 100;

cout << sqrt( n*1.0 ); 



cout << sqrt( n*1.0 );

                    ^^ 這樣n還是int資料型態

 

有二種方法:

  1. cout << sqrt((double)n);
  2. cout << sqrt(1.0 * n);     ※注意: sqrt(1.0 * n) 和 sqrt( n*1.0 )不一樣,一個是強制資料型態為double、一個是int

不甚了解,可否煩請解釋為何兩者會有差別?

 



抱歉我讀錯了

重翻回書,在type conversions那節裡說到一段

Conversions take place across assignments; the value of the right side is converted to the type of the left, which is the type of the result.

指的是像下面這個例子

int i;

char c;

 

 i  =   c ;

 c  i

上面的文字,是說藍色底色(右邊部分)的會轉換成黃色底色(左邊部分)的資料型態。

 

而我會指說 1.0 * n 和 n * 1.0 二個是不一樣的原因也是根據這段敘述

但實作的結果,發現是我屈解了文字,真是不好意思吐舌頭