The problem here, is that the computer, works
internally in a different number system called the
binary number system, and there's no exact representation
for 4.35 in that system. It's kind of
similar to your pocket calculator. When you divide
ten by three, you get 3.3333 and so
on. And then when you multiply again with
three, you would get 9.999999 and so on.
And the reason is that there is no
exact representation for the fraction ten over three
in the decimal system. Now as programmers, we
don't usually worry about the detail reasons why these
round of errors occur, we just have the
general intuition that floating point numbers are a little
bit fuzzy. Just like the fuzzy photo over
here, you can't quite rely on every last pixel.
So when you get a number like 434.999 and
so on, that's just something we have to deal with.
Now, in this case, if we don't want to
lose any pennies, the remedy would be to use integers
and work in pennies. If this had been 435
pennies as an integer, a 100 times 435 would not
have been a problem. There are several other number
types in Java that you may need in more technical
situations, we've prepared a fact sheet for you. Go ahead, read through
the worksheet, and then work with Sarah on a few quiz questions
Aquí el problema es que la computadora trabaja
internamente en un sistema de numeración diferente llamado
sistema de numeración binario, y no hay representación exacta
para 4.35 en ese sistema. Es
similar a tu calculadora de bolsillo Cuando divides
diez entre tres, obtienes 3.3333... y así
sucesivamente. Y después cuando lo multiplicas otra vez con
tres, obtendrías 9.999999 ... y así sucesivamente.
Y la razón es que no hay
una representación exacta de la fracción de diez más tres
en el sistema decimal. Ahora, como programadores,
normalmente no solemos preocuparnos a detalle por las razones del porqué estos
tipos de errores suceden, sólo tenemos
la intuición general de que los números de punto flotante son un
poco borrosos. Al igual que la foto borrosa aquí,
no puedes confiar tanto en cada pixel.
Así que cuando obtienes un número parecido a 434.999... y
sucesivamente, es algo con lo que tenemos que lidiar.
Ahora, en este caso, si no queremos
perder ningún centavo, el remedio podría ser utilizar números enteros
y trabajar en centavos. Si esto hubiera sido 435
centavos como un número entero, 100 por 435 no hubiera
sido un problema. Hay varios otros tipos
de números en Java que podrías necesitar en situaciones
más técnicas, hemos preparado una hoja de cálculo para ti. Adelante, lee
la hoja de cálculo y después trabaja con Sara en unas preguntas.
Le problème ici, c'est que l'ordinateur
fonctionne en interne dans
un système de différent
appelé système de numérotation binaire,
et il n'y a pas de représentation
exacte pour 4.35 dans ce système.
C'est un peu comme
votre calculatrice de poche.
Quand vous divisez 10 par 3,
vous obtenez 3,33333 et ainsi de suite
et puis quand vous multipliez
à nouveau avec 3,
vous obtiendrez 9,999999
et ainsi de suite.
Et la raison en est qu'il n'y a pas
de représentation exacte
pour la fraction 10 sur 3
dans le système décimal.
En tant que programmeurs,
nous ne nous préoccupons généralement
pas des raisons détaillées
pour lesquelles ces erreurs se produisent,
nous avons l'intuition générale
que les nombres à virgule flottante
sont un peu flous.
Tout comme la photo floue ici,
vous ne pouvez pas compter
sur chaque pixel.
Donc, quand vous obtenez un numéro
comme 434,999 et ainsi de suite,
c'est quelque chose que nous devons gérer.
Maintenant, dans ce cas, si nous
ne voulons pas perdre de centimes,
le remède serait d'utiliser
des entiers et de travailler en centimes.
Si cela avait été 435 pennies
comme un nombre entier,
100 fois 435 n'aurait pas été un problème.
Il existe plusieurs autres types
de nombres dans Java
qui vous seraient utiles dans
des situations plus techniques,
nous vous avons préparé
une fiche d'information.
Allez-y, lisez la fiche,
puis travaillez avec Sara
sur quelques questions quiz
ここでの問題はコンピュータが2進法と呼ばれる
システムを使って数を表していることです
このシステムでは4.35を
正確に表示することができないのです
一般的な電卓と同じですね
10を3で割ると3.3333… といった答えが得られます
その答えに再び3をかけると
答えは9.999999… になります
その理由はつまり10を3で割った数を
10進法で表示することはできないのです
なぜこのようなエラーが起こるのかについて
プログラマは詳細な理由を追及しません
ただ何となく浮動小数点数は
あいまいだという直感があるだけです
このピンボケの写真と同様です
1個1個のピクセルがはっきりと分かりませんね
434.999のような数字を得た場合には
解決しなくてはならない問題があるということです
この例ではわずかなお金も失いたくなければ
整数を使ってペニーを計算することが解決法です
これを整数になるように435ペニーとすれば
100×435で何も問題はありません
Javaではさらに技術を必要とする
数に関する型が他にもあります
ファクトシートを用意しましたので
読んでみてください
そしてサラと一緒に
いくつか練習問題をやってみましょう
컴퓨터의 숫자 체계는
2진수 체계로 되어 있어서 문제가 됩니다. 이 문제로 인해
4.35라는 숫자를 표현하지를 못합니다.
여러분의 계산기에도 같은 문제가
있습니다. 10을 3으로 나누면
3.333... 이라는 결과를
얻습니다. 또, 3으로 곱하면
9.999... 를 얻게 됩니다.
이렇게 되는 이유는 사실
십진수 체계에서 10 나누기 3에 대한
표현이 없어서 입니다. 프로그래머들은
이러한 오류들을 신경 쓰지 않고
실수형 숫자들은 이런 오류가 있구나 하고
받아들입니다. 조금 흐트러진 이 사진도
마찬가지 입니다.
모든 픽셀에 대해서 신경 쓸 수 없습니다.
그래서 434.999... 라는 숫자를 얻으면,
이건 그냥 받아들여야 합니다.
만약 이 경우에서 우리가 약간의 돈을 잃고 싶지 않다면,
가장 좋은 해결책은 정수형 자료형을 이용하는 겁니다.
이게 만약 435를 정수형으로 했다면,
100 곱하기 435는 큰 문제가 되지 않았을 겁니다.
자바에서는 훨씬 더 많은 숫자 자료형이 있습니다.
왜냐하면 여러 가지 상황에서 다르게 숫자를 표현해야 할
경우가 있기 때문입니다. 그래서 참고 물을 준비했습니다. 참고 물을
잘 보고 사라와 함께 퀴즈를 풀어보겠습니다.
Vấn đề ở đây, là máy tính,
bên trong đó làm việc trong một hệ thống số học khác,
gọi là binary number system (hệ thống số nhị phân), và không có cái gì chính xác để đại diện
cho số 4.35 trong hệ thống này. Nó cũng giống
tương tự như cái máy tính bỏ túi của bạn. Khi bạn chia 10 cho 3
bạn có được 3.333333 và tiếp tục.
Và sau đó bạn lại nhân lại với 3,
bạn lại có 9.999999 và tiếp tục.
Và lý do ở đây là
không có cái gì đại diện chính xác cho phân số 10/3
ở trong hệ thống nhị phân. Như các LTV,
ta không thường quan tâm về lý do chi tiết vì sao
vụ làm tròn lại bị lỗi như này, ta chỉ có
một hiểu biết thông thường đó là các số có dấu chấm động
có một sự sai số hơi nhẹ. Cũng như bức ảnh mờ mờ ở đây,
bạn không hẳn dựa vào mỗi pixel này được.
Nên khi bạn có một số kiểu như 434.999
và tương tự, đó chỉ là những thứ mà ta phải giải quyết.
Bây giờ, trong trường hợp này, nếu ta không muốn
mất bất kỳ đồng xu nhỏ nào, biện pháp khắc phục là sử dụng integer
và làm việc với các đồng xu. Nếu cái này là 435
đồng xu là một integer, 100 lần của 435
sẽ không có vấn đề. Có vài kiểu số trong Java
mà bạn có thể cần trong vài tình huống,
tôi đã chuẩn bị một cái bảng cho bạn. Tiếp tục, đọc nó,
và sau đó làm việc với Sarah với một vài quiz nhỏ.