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
在Java中用于
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…
分别是1、2、4、8、16、32等等
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.
就像0.1等于十分之一一样
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
是四分之三或0.75
is 3 over 4 or 0.75, plus
加上二的零次方即1等于
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
1.013×10²
times ten to the two. So
如果是二进制数101.11
if we had a binary number 101.11
可以表示为
That’s represented as 1.0111
1.0111×2²
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.
尾数占了23位
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
将值移位127
we’re shifting the value by 127.
如果这个值的指数是3
So if the value in the exponent is three,
这意味着127加3
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
都是0
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.
你可以给浮点数赋值为某数字、0
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
用10000表示16
the 16 with 10000.
小数部分是.001 最终二进制的表示形式是
The fractional part is .001 That makes the binary number
10000.001
10000.001
科学计数法中表示为
To represent that in our scientific notation
1.0000001×2⁴
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.
科学计数二进制数为1.0000001
Our scientific binary number is 1.0000001
第一个数字是1
We know the first digit is one, so
所以不用全部存储 小数部分只存000001
we don’t bother storing that. We just store 000001
剩下的全部为0
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
还记得尾数部分第一个数字一定是1
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.
让我们去创建几个float数和double数
Let’s go create some floats and doubles.
Java中创建这两个类型的变量和整型一样
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
它是float型
float.
如果不设置 Java会认为这是一个double类型
If you leave it off, Java thinks your literal is a double. You can add
你可以在字串后加“d”来明确一下
“d” to the end of your double literals for clarity, but the
但默认就是double
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
浮点数
number
或者是科学技术法表示的数字 可以用小写
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.
所以三分之一不能以10进制来表示
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.
用1除以二进制中的10(1010)
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.
我们打算输出16个十进制位的float值
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.
由于float和double适用于32和64位
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
有更好但会慢一些的BigDecimal类型
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 DeegeU.com 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 DeegeU.com.
官网首页上有一个投票
There’s a poll on the front page of DeegeU.com
你们可以通过它让我知道下节课的主题
so you can let me know what topic is covered next.
感谢收看 下次再见
Thanks for watching and see you in the next video.

发表评论

译制信息
视频概述

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

听录译者

收集自网络

翻译译者

谷子

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=Q2IYNfbYdqQ

相关推荐