abs 和 fabs 有什么区别? [英] What's the difference between abs and fabs?

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

问题描述

我在 python 上检查了 absfabs 之间的区别 此处

I checked the difference between abs and fabs on python here

据我所知,速度和传递的类型存在一些差异,但我的问题与 V.S. 上的本机 C++ 相关

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

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

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 [*]

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

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

我从第一个代码中了解到它不会编译,因为 fabs(-9) 需要一个 double,并且编译器无法将 -9 转换为 -9.0,但是在第二个代码中编译器会在编译时将 i=-9 转换为 i=-9.0 所以 fabs(i) 会正常工作.

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.

有更好的解释吗?

还有一点,为什么编译器不能接受 fabs(-9) 并像我们在 c# 中那样自动将 int 值转换为 double ?

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)   

推荐答案

在 C++ 中,std::abs 被重载用于有符号整数和浮点类型.std::fabs 只处理浮点类型(C++11 之前).注意 std:: 很重要;由于遗留原因而普遍可用的 C 函数 ::abs 只会处理 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!

问题

float f2= fabs(-9);

并不是没有从int(-9的类型)到double的转换,而是编译器不知道选择哪个转换 (int -> float, double, long double) 因为有一个 std::fabs 对于这三个中的每一个.您的解决方法明确告诉编译器使用 int ->double 转换,所以歧义消失了.

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 通过添加 double fabs( Integral arg ); 解决了这个问题,它将返回转换为 doubleabs>.显然,这个重载也可以在 C++98 模式下使用 libstdc++ 和 libc++.

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++.

一般来说,只要使用std::abs,它就会做正确的事情.(有趣的陷阱@Shafik Yaghmour.无符号整数类型在 C++ 中做了一些有趣的事情.)

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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆