本文介绍了Abs和fabs有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了 abs fabs 在python



据我了解,关于速度和传递的类型有些区别,但是我的问题与VS上的本机c ++有关



关于VS
我在 Visual Studio 2013(v120)上尝试了以下操作:

  float f1 = abs(-9.2); // f = 9.2 
float f2 = fabs(-9); //编译错误[*]

所以 fabs(-9)会给我一个编译器错误,但是当我尝试执行以下操作时:

  double i =- 9; 
float f2 = fabs(i); //这可以正常工作

我从第一个代码中了解到,由于 fabs(-9)需要加倍,并且编译器无法将-9转换为-9.0,但是在第二个代码中,编译器将转换 i =- 9 i = -9.0 ,因此 fabs(i)可以正常工作



还有更好的解释吗?



另一件事,为什么编译器不能接受 fabs(-9)并将int值转换为像c#一样自动加倍?

  [*]:

错误:一个以上的重载函数 fabs实例与参数列表匹配:
函数 fabs(double _X)
函数 fabs (float _X)
函数 fabs(long double _X)
参数类型为:(int)


解决方案

在C ++中, std :: abs 对于有符号整数和浮点po均重载。 int类型。 std :: fabs 仅处理浮点类型(C ++ 11之前的版本)。请注意, std :: 很重要,出于遗留原因通常可用的C函数 :: abs 处理 int





<$ p $的问题p> float f2 = fabs(-9);

不是说 int ( -9 的类型)到 double 的类型,但是编译器不知道选择哪个转换( int -> float double long double ),因为这三个都各有一个 std :: fabs 。解决方法明确地告诉编译器使用 int -> double 转换,这样就消除了歧义。



C ++ 11通过添加 double fabs(Integral arg); 来解决此问题,这将返回任何整数类型的abs 都转换为 double 。显然,这种过载在libstdc ++和libc ++的C ++ 98模式下也可用。



通常,只需使用 std :: abs ,它将做正确的事情。 (有趣的陷阱 / 1708801 / shafik-yaghmour> @ Shafik Yaghmour 。无符号整数类型在C ++中会很有趣。)


I checked the difference between abs and fabs on python here

As I understand there are some difference regarding the speed and the passed types, but my question related to native c++ on V.S.

Regarding the V.S.I tried the following on Visual Studio 2013 (v120):

float f1= abs(-9.2); // f = 9.2
float f2= fabs(-9); // Compile error [*]

So fabs(-9) it will give me a compiler error, but when I tried to do the following:

double i = -9;
float f2= fabs(i); // This will work fine

What I understand from the first code that it will not compile because fabs(-9) need a double, and the compiler could not convert -9 to -9.0, but in the second code the compiler will convert i=-9 to i=-9.0 at compile time so fabs(i) will work fine.

Any better explanation?

Another thing, why the compiler can't accept fabs(-9) and convert the int value to double automatically like what we have in c#?

[*]:

Error: more than one instance of overloaded function "fabs" matches the argument list:
        function "fabs(double _X)"
        function "fabs(float _X)"
        function "fabs(long double _X)"
        argument types are: (int)
解决方案

In C++, std::abs is overloaded for both signed integer and floating point types. std::fabs only deals with floating point types (pre C++11). Note that the std:: is important, the C function ::abs that is commonly available for legacy reasons will only handle int!

The problem with

float f2= fabs(-9);

is not that there is no conversion from int (the type of -9) to double, but that the compiler does not know which conversion to pick (int -> float, double, long double) since there is a std::fabs for each of those three. Your workaround explicitly tells the compiler to use the int -> double conversion, so the ambiguity goes away.

C++11 solves this by adding double fabs( Integral arg ); which will return the abs of any integer type converted to double. Apparently, this overload is also available in C++98 mode with libstdc++ and libc++.

In general, just use std::abs, it will do the right thing. (Interesting pitfall pointed out by @Shafik Yaghmour. Unsigned integer types do funny things in C++.)

这篇关于Abs和fabs有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 05:51