简体   繁体   English

x * 0.1 和 x / 10 之间的差异?

[英]Difference between x * 0.1 and x / 10?

I am a high school student and I saw a while ago that is better to multiply than to divide.我是一名高中生,不久前我看到乘法比除法好。 Since then, without any proof found that it is true or not and without knowing how to do find it by myself at the moment, I tried to modify my codes for that slightly better time.从那以后,在没有任何证据证明它是真是假的情况下,我现在也不知道如何自己找到它,我试图修改我的代码以适应那个稍微好一点的时间。

Here is a problem where I wanted to find the biggest digit in a number using recursion.这是一个问题,我想使用递归找到数字中的最大数字。

This one is working.这个正在工作。

#include <iostream>

using namespace std;

int minn = 9;

int digit(int n)
{
    if(minn > n % 10)
        minn = n % 10;
    if(!(n / 10))
        return minn;
    else
        return digit(n / 10);
}

int main()
{
    int x;
    cin >> x;
    cout << digit(x);
    return 0;
}

But this is not working.但这不起作用。

#include <iostream>

using namespace std;

int minn = 9;

int digit(int n)
{
    if(minn > n % 10)
        minn = n % 10;
    if(!(n * 0.1))
        return minn;
    else
        return digit(n / 10);
}

int main()
{
    int x;
    cin >> x;
    cout << digit(x);
    return 0;
} 

The only difference is that the broken one use if(.(n * 0.1)) not if(!(n / 10)) .唯一的区别是损坏的一个使用if(.(n * 0.1))而不是if(!(n / 10))

Can someone clarify it for me or anyone who is seeking help what is the difference between x * 0.1 and x / 10?有人可以为我或任何寻求帮助的人澄清一下 x * 0.1 和 x / 10 有什么区别?

Thank you all for clicking the question and that you tried to help!感谢大家点击问题并尝试提供帮助!

0.1 is a double type, 10 is an integer type. 0.1是双精度型, 10是 integer 型。

When dividing two integers eg n / 10 you will get integer division (eg 6/10 will equal 0 ).将两个整数相除时,例如n / 10 ,您将得到 integer 除法(例如6/10将等于0 )。

And your check will work differently when using 6 * 0.1 as that will equal 0.6 .使用6 * 0.1时,您的检查将有所不同,因为这将等于0.6

n *.1 results in a floating point result. n *.1产生浮点结果。 So, 3 *.1 produces the result of .3 , which is definitely not 0.因此, 3 *.1产生.3的结果,这绝对不是 0。

On the other hand, 3/10 is 0. That's how integer division works in C++.另一方面,3/10 为 0。这就是 integer 除法在 C++ 中的工作方式。

Can someone clarify it for me or anyone who is seeking help what is the difference between x * 0.1 and x / 10?有人可以为我或任何寻求帮助的人澄清一下 x * 0.1 和 x / 10 有什么区别?

Difference is very significant.差异非常显着。 In the first case you use floating point multiplication in the second integer division.在第一种情况下,您在第二个 integer 除法中使用浮点乘法。 So first if() would happen when n == 0 while second when n < 10.所以第一个if()会在 n == 0 时发生,而第二个会在 n < 10 时发生。

if( !(n*0.1) ) // for if to work result of n * 0.1 must be equal 0 which only happens when n == 0

if( !(n/10) ) // for if to work result of n / 10 must be equal to 0 which happens when abs(n) < 10 (including 0)

You define x as int , x*0.1 uses float arithmetics, while x/10 uses integer arithmetics您将 x 定义为intx*0.1使用浮点算术,而x/10使用 integer 算术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM