为什么在Java中以稍微不同的形式书写的两个相同数字会产生两个不同的结果

huangapple go评论58阅读模式
英文:

Why do 2 same numbers written in slightly form produce 2 different outcomes in Java

问题

我成功使用Java代码解决了一个数学问题。然而,在这个过程中,我也偶然发现了一些奇怪的事情。

在我的计算中,我不得不相加4个数字:13、132、320和201。我声明了一个int变量sum,并将其初始化为13 + 132 + 320 + 201。

int sum = 13 + 132 + 320 + 201;

当我将变量sum输出时,它返回一个值为666。这是有道理的,因为在计算器上相加这些数字会得到那个值。然而,我决定将变量sum设置为一个稍微不同的值。我决定将sum设置为013 + 132 + 320 + 201。

sum = 013 + 132 + 320 + 201;

然而,当我输出这个值时,我得到了664。我决定在013的左边再加一个零。

sum = 0013 + 132 + 320 + 201;

而sum返回了相同的值,664。

因此,基本上,只要我像那样添加数字而没有任何不必要的零,sum就会返回正确的值。但是当我添加这些不必要的零时,sum会返回一个稍微不同的答案。是不是因为在数字之前加零会导致稍微不同的结果呢?

英文:

I successfully managed to solve a math problem using Java code. However, in doing so, I've also stumbled upon something weird.

In one of my calculations, I had to add 4 numbers: 13, 132, 320, and 201. I declared an int variable sum, and initialized it to 13 + 132 + 320 + 201.

int sum = 13 + 132 + 320 + 201;

When I printed the variable sum out, it returned a value of 666. Which makes sense, since adding those numbers on a calculator returns that value. However, I decided to then set the variable sum equal to something a little different. I decided to set sum equal to 013 + 132 + 320 + 201.

sum = 013 + 132 + 320 + 201;

However, when I printed this value out, I got 664. I decided to add one more zero to the left of 013.

sum = 0013 + 132 + 320 + 201;

And sum returned the same value, 664.

So basically, whenever I add the numbers just like that without any unnecessary zeroes, sum returns the correct value. But when I add those unecessary zeroes, sum returns a slightly different answer. Is there a reason as to why putting zeroes before a number causes a slightly different result?

答案1

得分: 3

在数学中,仅有基数是非十进制的。因此,为了定义16进制(Hexa-decimal)、8进制(Octal)、2进制(Binary)和10进制(Decimal)中的数字,必须有一种方式。在Java中,您可以像下面这样定义它们。

通过添加前缀0b或0B,您可以定义二进制数字(基数2)

byte b1 = 0b101;

通过添加前缀0,您可以定义八进制数字(基数8)

int octal = 013;

通过添加前缀0x,您可以定义十六进制数字(基数16)

int hexaDecimal = 0x13;

您可以在不使用任何前缀的情况下定义十进制数字

int decimal = 13;

英文:

In math only base is not decimal .So to define numbers in base 16(Hexa-decimal),8(Octal),2(Binary) and 10(Decimal) there must be a way.So in java you can define those like below.

Adding the prefix of 0b or 0B you can define binary numbers (base 2)

byte b1 = 0b101;

Adding the prefix of 0 you can define octal numbers(base 8)

int octal = 013;

Adding the prefix of 0x you can define hexa decimal numbers(base 16)

int hexaDecimal =0x13;

You can define decimal numbers without using any prefix

int decimal = 13;

答案2

得分: 1

你的问题基本上是这样的:

// Java将其解释为八进制数字
int octal = 013;
// Java将其解释为十六进制数字
int hexa = 0x13;
// Java将其解释为十进制数字
int decimal = 13;
英文:

Your question is basically this:

// Java interprets this as octal number
int octal = 013;
// Java interprets this as hexadecimal number
int hexa = 0x13
// Java interprets this as decimal number
int decimal = 13

huangapple
  • 本文由 发表于 2020年10月20日 23:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64447935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定