ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#10 Java浮点数类型 – 译学馆
最新评论 (0)

《迪哥Java教程》#10 Java浮点数类型

Java floating-point number intricacies - 010

So what happens when we need to represent numbers and fractions?
目前为止 我们只介绍了整型 对于实数来说
So far we’ve covered integer types only. For real numbers
有两种数据类型可以表示小数 它们有不同的取值范围
we have two data types for fractional numbers, and again in different sizes.
它们就是float和double float是较短的浮点数
The two data types are float and double. Float is short for floating-point
double就是双倍尺寸的浮点数 这些基本类型
and double just means double size floating-point. These primitives are used
to represent
real numbers in Java. Our
goals for this lesson are to learn about floating point numbers,
也被称为实数 分数 小数
also known as real numbers, fractional numbers, decimal numbers
或连续数轴上的数字 你也会学习到浮点数
or numbers from a continuous number line. You’ll also learn about how floating
point numbers are stored
整型有什么不同 你会了解到计算机非常擅长处理整数
differently than the integers. You’ll learn that computers are great for whole numbers,
但当你要处理小数时 它们的表现却是非常糟的
but they can really suck when you try and add decimal numbers.
So were finally gonna talk about when floating point numbers go bad.
Before we dive into the floating-point numbers
来复习一下 我们可以把数字写成二进制形式
let’s review. We can represent numbers as binary numbers.
它们和十进制遵循相同的模式 代替10的幂次方的是
They follow the same pattern as decimal numbers. Instead of being to the power of ten,
2的幂次方 所以代替1、10、100、1000等等的
it’s to the power of two. So instead of 1, 10, 100, 1000…
etc., each bit is 1, 2, 4, 8, 16, 32…
and so on. We covered this
in the lesson, “How do computers store numbers?”. Binary numbers work the same way
for fractions.
just as 0.1 is really
one over ten, the first fractional bit for a binary number
是二分之一 之后是四分之一
is one over two. Then it’s one over four,
八分之一等等 它们都是二的幂次方
one over eight, and so on. Which are just powers of two.
So if we took
1.75 在二进制中是
1.75 decimal, in binary
1.11 整数部分的1
it would be 1.11. Whole one is
是二的零次方 小数部分第一个数字是二分之一
two to the power 0, the first fractional digits is one over two,
第二个是四分之一 二分之一加上四分之一
and the second is one over four. One over two plus one over four
is 3 over 4 or 0.75, plus
two to the 0 power for our 1 equals
1.75 还记得科学技术法吧
1.75. If you remember scientific notation,
对二进制也适用 以十进制数
that works too. Take the decimal number
101.3 为例 即
101.3. That’s 1.013
times ten to the two. So
if we had a binary number 101.11
That’s represented as 1.0111
times 2 to the second power.
You’re probably asking what does this all mean for floating-point numbers?
float可以表示从…… 嗯
The float can represent numbers from… well
这个到那个 [看上面]
this one to that one. [see above]
这个数可以是正或负 比特排序与在整型中的
The numbers can be positive or negative. The bits are ordered a different way than
they were for integers.
它分为三部分 符号位 指数部分
It’s got three parts, the sign, the exponent,
尾数部分 第一位
and the mantissa. The first bit says
代表一个数的正负 即符号位
if a number is positive or negative. Thats the sign bit.
The mantissa is our number in 23 bits,
normalized as a scientific notation number.
指数占8位 和二进制科学计数法中的指数类似
The exponent is eight bits, and it’s like the exponent in our binary scientific notation,
but the exponent is biased. The word
“有偏”是一个工程上的概念 意味着在这种场景
biased is an engineering thing. What it means in this case is
we’re shifting the value by 127.
So if the value in the exponent is three,
what it really means is 127 plus three,
也就是130 那么为什么要这样做呢?
or 130. So why would you want to do that?
因为我们也想同时支持负指数 那是表示
Well we want negative exponents too, and this way we can represent
fractional numbers that have no whole part.
There’s a couple of other numbers you can represent.
如果所有的都是0 数字也是0
If everything is 0, then the number is 0.
符号位也没有影响 如果符号位被设置为1
The sign bit doesn’t really matter. If the sign bit is set,
其他位都是0 结果仍然为0
and everything else is 0, that is still 0. Sometimes
有时会被叫做负0 如果所有的指数位都被设置为1 但尾数位都是
that’s called negative 0. If all the exponent bits are set, but the mantissa bits are
all zero,
这是个特别的数字 无穷大
that’s a special number. That’s infinity.
The sign bit tells us if it’s positive or negative infinity.
If all the exponent bits are set
且至少一个尾数位被赋值 这又是一个特殊值
and at least one mantissa bit is set, that’s another special number
叫做NaN(非数字) 表示
called NaN. It stands for
不是数字 当除以0或求负数平方跟时
not a number. It’s useful for when you divide by zero or take the square root
of a negative number.
It doesn’t matter which mantissa bit or how many are set.
Now you can set you’re floating point number to and number, zero,
或无穷大、非数字 我们来用浮点数表示16.125
infinity, or not a number. So let’s represent 16.125 using a float.
float是32位数字 我们可以
Float is a 32-bit number. We can represent
the 16 with 10000.
小数部分是.001 最终二进制的表示形式是
The fractional part is .001 That makes the binary number
To represent that in our scientific notation
we have 1.0000001 binary times two to the fourth.
现在我们已经设置完了所有浮点数的所有位 符号位是零
So now we have to place this all in our floating-point bits. The sign is positive
代表一个正数 0在32位
so that zero. 0 goes into the 32 bit.
指数是4 记住我们需要让该指数“有偏”
The exponent is four. Remember we need to bias this number so
那么 要用127加4
that makes our exponent 127 plus four.
也就是131 把131存储到
Or 131. We store 131
第24到31位 就是这个数字
into bits 24 to 31, And that’s this number.
The last bit is the mantissa.
Our scientific binary number is 1.0000001
We know the first digit is one, so
所以不用全部存储 小数部分只存000001
we don’t bother storing that. We just store 000001
for the mantissa and the rest the digits are zero.
So there’s our final number. And that
is what gets stored into a float. A double
double只是一个大一些的float 用一位表示符号位
is just a bigger float. It uses one bit for the sign,
11位是指数位 剩下的53位是尾数
11 bits for the exponent, and 53 bits for the mantissa.
精度更高 取值范围更大 屏幕前的朋友们或许在想
It gives you more precision and bigger values. So those are you paying attention at home are thinking,
等等 1加11加53等于65
wait! 1 plus 11 plus 53, that’s 65.
但这是一个64位数字 怎么会这样?
This is a 64-bit number. What gives? Well
if you remember the first digit of the mantissa is always one.
我们不用存储它 它是一个隐藏的位 这是我们使用科学技术法的原因
And we never store it. It’s an implied bit. That’s why we use the scientific notation.
第一位始终是1 我们可以直接得到
That way we know the first bit is always one and we get it for free.
Let’s go create some floats and doubles.
To create the types in Java, we use the same pattern we did for integer types.
类型设置为float或者double 然后起个名字
We give the type, either float or double, we give it a name
然后设置一个可能的值 当定义float类型变量时
for our number, and then possibly value. When defining a float
你需要在字串最后添加字母f 这可以告诉java
you need to add the letter “f” at the end of your literal. That tells Java it’s a
如果不设置 Java会认为这是一个double类型
If you leave it off, Java thinks your literal is a double. You can add
“d” to the end of your double literals for clarity, but the
default is just double.
幸运的是 创建一个数字时 我们不用亲自进行位操作
Luckily, we don’t have to do all that bit manipulation to create our numbers.
我们可以分配一个十进制的字串给float类型变量 或者一个
We can assign our float a decimal literal, either as a floating point
或者是科学技术法表示的数字 可以用小写
or as a number in scientific notation. You can use a lowercase
或大写的“e” 这是一个Java会忽略大小写的特殊情况
or uppercase “e”. In this particular case, Java is case-insensitive.
运行程序 输出数字
We run the app, and it prints our numbers back out.
让我们制造一个错误出来 把double变量赋值成float值
Let’s go try creating that error where we assign a double to float.
创建一个float变量 赋值但是在末尾不带“f”
We’ll try creating a float, then assigning a number without the “f” the end.
会再次出现一个有损转换错误 没关系 因为大部分情况
We get that lossy conversion error again. That’s ok though because, most the time
你使用的是double类型 目前已经很少有人用
you’re gonna be working with doubles. There’s really little use
float类型了 刚才我们讨论的
for using floats these days. You can also set the numbers to the special
那些特定数字 你也可以
numbers we talked about before.
使用Float和Double类型来赋值 我们来把值设置为NaN
Using the Float and Double number classes. Here’s what we do to set it as a NaN.
一会我们会介绍到这几个类 我们可以把这个变量赋值为负无穷
We’ll cover these classes later. We can set this to negative infinity,
我们可以以同样的方式处理double类型的值 当运行程序时
And we can do the same for doubles. When we run
会打印出该值的字符串形式 也许 你已经注意到
it, it prints out the string for the value. You probably noticed
如果一些数字被用于表示指数 那么他们不能被用于
if some of the digits are used to represent exponents, they can’t be used
表示个别的数字 如你在整型中所了解的一样
represent a particular individual number, you know like in integers.
This means the you have holes in your number line. Its
不可能表示出所有的数字 一些浮点数字
just not possible to represent all numbers. Some floating point numbers
不能用float或double来表示 如果不能表示
just can’t be represented in a float or a double. And if it can’t be represented
it gets rounded to the nearest number that it can represent.
This problem is even worse when you consider that
some simple numbers can’t be represented at all.
Take the number one over three or one-third.
当你用一个小数来表示时 它是一个无限循环小数
When you try to represent this is a fraction it goes on forever.
我们称之为循环小数 在二进制中会
This is what we call a “repeating decimal”. You can have the same problem with
binary numbers.
So one over three doesn’t work in decimal.
Take 1 over 10, or one tenth.
十进制数字是0.1 在二进制中试试
The decimal number is 0.1 Try this in binary.
Take 10 in binary and divide by one.
你会发现 你会得到二进制的无限循环小数并且这个数字永远
You’ll find that you get a repeating pattern in binary and the number will
never end.
这点需要牢记 尤其是在表示钱的时候
This is something to remember, especially when you trying to represent money.
不能表示10美分 听起来不可思议
You can’t represent ten cents! That sounds crazy!
看看实际的操作 假入我们要用10美分
So let’s see this in action. Pretend we’re adding
加上20美分 按理说应该是30美分
10 cents with 20 cents. That should give us 30 cents.
We’re going to format the print statement,
because the normal print statement is going to round the results.
We’ll print out the 16 decimal places for the float,
以及17个十进制位的double值 运行程序
and 17 for the double. Run the program
虽然得到30美分 但是在末尾处附加了
and we get thirty cents plus
一些随机的数字 加的值越大
some random digits way out there. The more you add,
误差越大 永远都不要用float类型的数
the more that error will move to the right. You should never ever use a primitive
floating point number for money.
So you’re probably wondering why use them at all if they are so
error prone? The floating-point primitive types are
fast. You’re exchanging accuracy for speed.
Since floats and doubles fit into 32 and 64 bits,
它们被计算机指令充分利用 CPU指令在几个时钟周期内去
they are optimized for the instructions on your computer. There are instructions on your
CPU to handle these data types in a few clock cycles.
You’ll often see these types used for graphics and for games.
Java 中大部分数学程序都使用double类型
Most of the math routines in Java are also using doubles.
所以 如果不受限于内存 double类型优先
So, if you’re not constrained for memory, always prefer the double.
你会得到更高的精度 而且对当今的计算机来说基本上看不出来
You get a bit more accuracy and the performance penalty really doesn’t show
in modern computers.
这就是float和double 如果你需要更高的精度
So that’s the float and the double. There’s a much better, but
slower, thing you can use if you want more accuracy called the BigDecimal.
当我们学习基本类的时候会介绍到 下一节课
We’ll cover that later when we get to primitive classes. Up next however
我们将学习boolean和char类型 这样所有的基础类型就都讲完了
we’ll look at the boolean and char primitives. And that will wrap it up for primitives.
嗨!感谢收看 如果你想检查下这节课的学习效果
Hey! Thanks for watching the video. There’s a quick quiz for this on if
you’d like to gauge how much you’ve learned.
如果喜欢这个系列的视频 请为这个视频点赞或
If you like the videos you are seeing, please let me know by liking the video and
点击YouTube上DeegeU频道的订阅按钮来让我知道 非常感谢
hitting the subscribe button for the DeegeU channel on YouTube. I’d really appreciate
如果你有任何疑惑或问题 请在下面的评论区或
that. If you have concerns or questions please leave them in the comments below
or on
There’s a poll on the front page of
so you can let me know what topic is covered next.
感谢收看 下次再见
Thanks for watching and see you in the next video.



介绍了两种浮点类型的存储方式,取值范围 使用禁忌