หนึ่งในปัญหาคลาสิก เวลาเขียนโปรแกรมที่ทุกคนต้องเจอเลย
ก็คือการบวกลบเลขทศนิยมในภาษาโปรแกรม ของบางภาษา นี้แหละ
เช่น JavaScript, Python, Perl, C#, C, C++, Java, PHP, Fortran
(และอื่นๆ อีกหลายภาษาที่ไม่ได้กล่าวถึง)
.
หลายครั้งที่มันอาจเพี้ยนได้ เช่น
👉 0.1+0.2 ไม่ได้เท่ากับ 0.3
แต่ได้เป็น 0.30000000000000004
.
👉 หรือ 0.1 บวกกัน 10 ครั้ง ก็ไม่ได้เป็น 1
แต่ได้เป็น 0.9999999999999999
.
คนเขียนโปรแกรมเจอแบบนี้เข้าไป
ก็เหมือนมวยโดนหมัดน๊อคมึนงงในดงโค้ด
:
:
แต่ใช่ว่ามันจะเพี้ยนทุกครั้ง ซะเมื่อไร เช่น
0.5+0.5 = 1 (ถูกต้องเป๊ะ)
0.2+0.3 = 0.5 (บังเอิญไม่เพี้ยน)
.
สำหรับ กรณี 0.2 กับ 0.3 มันถูกตัดเศษเหลือเป็น
0.2000000000000000111022302462515654042363166809082031250
กับ
0.2999999999999999888977697537484345957636833190917968750
พอบวกกันจึงได้ 0.5 พอดี แบบฟลุ๊คๆๆ ซึ่งไม่ควรทำได้
(ตรงสอบดูได้ 0.2+0.3 == 0.5 ได้ค่าออกมาเป็น true)
:
:
สาเหตุที่เป็นเช่นนี้
ก็เพราะว่าคอมพิวเตอร์มันรู้จักแต่ เลขฐาน2 อะนะ
ต่อให้เราเขียนโค้ดใช้เลขฐาน10 ก็ตาม
สุดท้ายเวลาโค้ดมันถูกรัน ก็จะกลายเป็นเลขฐาน 2 อยู่ดี
.
😨 แล้วก็เป็นความซวยที่จะมาเยือนคนเขียนโปรแกรม
เพราะเวลาแปลงเลขฐาน10 ไปเป็นเลขฐาน 2
บางกรณีมันแปลงแล้ว ดันได้ตัวเลขที่ไม่รู้จบเสียด้วยซิ
จึงทำให้การเก็บทศนิยมผิดเพี้ยนไปได้
.
สำหรับรูปแบบการจัดเก็บเลขทศนิยม ในหลายภาษา
เขาจะนิยมใช้มาตรฐาน IEEE-754 floating point
เช่น 0.1 จะถูกมองว่าคือ 1/10
.
เมื่อเก็บเป็นเลขทศนิยมฐานสอง
ตามมาตรฐาน IEEE-754 floating point จะได้เป็น
0.0001100110011001100110011001100110011001100110011...
เป็นทศนิยมไม่รู้จบในรูปเลขฐานสอง ....นี้คือสิ่งที่คอมมองเห็น
.
พอคอมแปลงกลับมาเป็นทศนิยม เพื่อให้มนุษย์โลกอ่านเข้าใจ
ในรูปฐาน 10 ก็จะได้เป็น
0.1000000000000000055511151231257827021181583404541015625
ทว่าคอมมันจะตัดให้เหลือแค่ 0.1 (คนจึงเห็นแค่นี้)
:
🤔 ซึ่งความเพื้ยนแบบนี้
แน่นอนทำให้เกิดบั๊กเวลาคำนวณตัวเลข
- ยิ่งงานต้องการคำตอบที่ละเอียดมาก เช่น งานธนาคาร ก็จะประสบปัญหา เป็นต้น
- หรือเวลานำไปใช้ในเงื่อนไขเปรียบเทียบพวก if, while ฯลฯ ก็อาจมีบั๊กเกิดขึ้นได้ เป็นต้น
.
😀 แต่ไม่ต้องห่วง ในหลายๆ ภาษาเขาจะมีวิธีแก้ปัญหานี้อยู่ครับ
ป้องกันการคำนวณตัวเลข ไม่ให้คลาดเคลื่อน เช่น
- ใน Java ก็จะมีคลาส BigDecimal เอาไว้บวกลบคูณหาร สำหรับเลขทศนิยมโดยเฉพาะ
- ใน Python ก็จะมีคลาสคล้ายๆ กัน เช่น Decimal
- ส่วนใน JavaScript อาจใช้ไลบรารี่ ซึ่งมีให้เลือกเยอะเช่น
https://github.com/MikeMcl/decimal.js/
https://github.com/MikeMcl/bignumber.js/
https://github.com/MikeMcl/big.js/
- ภาษาอื่นที่เหลือลองไปศึกษาเองดูนะครับ
.
.
เรื่องบวกลบคูณหาร เลขทศนิยม ถือเป็นเรื่องสำคัญที่ไม่ควรมองข้าม
โดยส่วนตัวก็เคยเจอความเผลอเรอตรงนี้
ในระดับโปรเจคระดับธนาคาร ก็เคยพลาดมาแล้ว
สุดท้ายต้องมาไล่นั่งแก้โค้ดหลายบรรทัด
เสียเวลานั่งไล่ test ใหม่อีกรอบอีก
.
หมายเหตุเห็นคอมเมนต์สงสัยว่า
PHP กับ C# รอดชะตากรรมเดียวกันไหม ?
ก็บอกว่าไม่รอดครับ
.
// ลองดูตัวอย่างโค้ด C#
Console.WriteLine( ((0.1+0.2) == 0.3)); // False
Console.WriteLine( ((0.1+0.2) == 0.30000000000000004)); // True
// ลองดูตัวอย่างโค้ด PHP
echo number_format(0.1+0.2 , 17);
.
++++++
เขียนโดย โปรแกรมเมอร์ไทย thai programmer
อ่านเรื่อง IEEE-754 floating point ได้ที่
https://th.wikipedia.org/wiki/จำนวนจุดลอยตัว
One of the programming time class issues that everyone needs to encounter.
It's a positive, negative, decimal number in the programming language of some languages.
เช่น JavaScript, Python, Perl, C#, C, C++, Java, PHP, Fortran
(And many other languages not mentioned)
.
So many times it can be crazy like
👉 0.1 + 0.2 is not equal to 0.3
But got to be 0.30000000000000004
.
👉 or 0.1 plus 10 times. It's not 1
But got to be 0.9999999999999999
.
The programmers found this.
It's like boxing. I got a punch. I'm confused in the code.
:
:
But it's not crazy every time.
0.5 0.5 0.5 0.5 1 (Exactly correct)
0.2 0.2 0.3 0.3 0.5 (accidentally not crazy)
.
For 0.2 and 0.3 cases, it was cut as debris.
0.2000000000000000111022302462515654042363166809082031250
With
0.2999999999999999888977697537484345957636833190917968750
Let's be positive. I got 0.5 fits. Fluke which I shouldn't do.
(I can see the exam. 0.2 + 0.3 == 0.5 I got the value to be true)
:
:
The cause is like this
It's because computer only knows the base number 2
Even if we write code, use base number 10
Finally, when the code is run, it will become the base number 2 anyway.
.
😨 and it's bad luck to visit the programmers.
Because time converts base number 10 to base number 2
In some cases, it's converted. I get an endless number.
So that the decimal collection is wrong.
.
For decimal numbers storage in multiple languages
He will be popular with IEEE-754 floating point standards.
For example, 0.1 will be seen as 1/10
.
When it's kept as a decimal number, binary digits.
According to IEEE standards-754 floating point will be.
0.0001100110011001100110011001100110011001100110011...
It's an endless decimal in the second base number.... This is what the computer sees.
.
When the computer comes back to a decimal, so that the world can read and understand.
In the base photo, 10 will be.
0.1000000000000000055511151231257827021181583404541015625
But the computer will cut it down to 0.1 (that's all I see)
:
🤔 This kind of friendship
Definitely make a time bug. Calculates numbers.
- The more jobs require a detailed answer, such as banking job, the problem is etc.
- or time to apply in comparison terms. If, while etc, there may be a buck happening. etc.
.
😀 But don't worry. In many languages, there will be a solution to this problem.
Prevent calculation of numbers from discrepancy, e.g.
- In Java, there will be a BigDecimal class. Plus, multiply, multiply for decimal numbers.
- In Python there are similar classes like Decimal
- Parts in JavaScript may use a lot of library to choose from, e.g.
https://github.com/MikeMcl/decimal.js/
https://github.com/MikeMcl/bignumber.js/
https://github.com/MikeMcl/big.js/
- Other languages. Let's study it yourself.
.
.
A positive, multiply, digging, decimal numbers are important things that shouldn't be overlooked.
Personally, I have experienced the accident.
Bank level project. I have already missed it.
Finally, I have to sit and solve many lines of code.
Waste of time. Sit to chase the new test again.
.
Note, see comments, wonder if
PHP and C #survive the same fate?
I told you that you won't survive.
.
// Check out the C code trailer #
Console.WriteLine( ((0.1+0.2) == 0.3)); // False
Console.WriteLine( ((0.1+0.2) == 0.30000000000000004)); // True
// Check out the PHP code trailer
echo number_format(0.1+0.2 , 17);
.
++++++
Written by Thai programmer thai coder
Read IEEE-754 floating point at
https://th.wikipedia.org/wiki/จำนวนจุดลอยตัวTranslated
同時也有1部Youtube影片,追蹤數超過11萬的網紅まるん Marun,也在其Youtube影片中提到,Minecraft街づくりシリーズ、今回は裏通りからつながるマンションの入口&階段部分をつくりました! ◇チャンネル登録 SUB https://www.youtube.com/channel/UC2sSgO3jqJxOMAyf2wJlOTg ◇メンバー登録 JOIN https://gamin...
「make java library」的推薦目錄:
- 關於make java library 在 โปรแกรมเมอร์ไทย Thai programmer Facebook 的最讚貼文
- 關於make java library 在 まるん Marun Youtube 的精選貼文
- 關於make java library 在 How to create my own java library(API)? 的評價
- 關於make java library 在 Creating and using your own external Java library ... - YouTube 的評價
- 關於make java library 在 How to Make a Processing (Java) Library Part 1 - YouTube 的評價
- 關於make java library 在 Maintaining a medium-sized Java library in 2022 and beyond 的評價
- 關於make java library 在 When creating a library, is there a way to make certain ... 的評價
- 關於make java library 在 java-library · GitHub Topics 的評價
make java library 在 まるん Marun Youtube 的精選貼文
Minecraft街づくりシリーズ、今回は裏通りからつながるマンションの入口&階段部分をつくりました!
◇チャンネル登録 SUB
https://www.youtube.com/channel/UC2sSgO3jqJxOMAyf2wJlOTg
◇メンバー登録 JOIN
https://gaming.youtube.com/channel/UC2sSgO3jqJxOMAyf2wJlOTg
※月額490円でチャンネルの応援ができます♪
◇ゲーム実況 Marun Games
https://www.youtube.com/channel/UCt3Lyv90yDECZK345l44NpQ
◇Twitter
https://twitter.com/thatmarun
- - - - - - - -
◆街づくり (Build a City)
https://www.youtube.com/playlist?list=PLm2ap0jegELN3xQxiR-wnlgmxKrjOKpSf
◆和風建築 (Japanese style building)
https://www.youtube.com/playlist?list=PLm2ap0jegELMfKMT_iDWwdysu6O20jrO0
◆農業マイクラ
https://www.youtube.com/playlist?list=PLm2ap0jegELPah8ycor-NChsStZ9OcN0e
- - - - - - - -
このチャンネルでは主にマインクラフトで建築をしています。ぜひ動画への高評価&チャンネル登録よろしくお願いします。今後の活動の励みになります😊
In this video, I play Minecraft: Java Edition. I love building houses! If you like this video, give it a thumbs up👍 If you want to watch more Minecraft gameplay videos like this, don't forget to subscribe :)
#Minecraft #マインクラフト #マイクラ
- - - - - - - -
Music◆音楽
YouTube Audio Library
DOVA-SYNDROME
Shaders◆シェーダー・影MOD
KUDA Shaders
SEUS
![post-title](https://i.ytimg.com/vi/LMzG8B1dHhs/hqdefault.jpg)
make java library 在 Creating and using your own external Java library ... - YouTube 的推薦與評價
![影片讀取中](/images/youtube.png)
In this video, we will see how to create your own external Java Libraries and how to use them in your Raspberry Pi and TotalCross projects. ... <看更多>
make java library 在 How to Make a Processing (Java) Library Part 1 - YouTube 的推薦與評價
![影片讀取中](/images/youtube.png)
Have you ever wanted to learn more about Java programming, how open source works, or how to build your own Processing library ? ... <看更多>
make java library 在 How to create my own java library(API)? 的推薦與評價
... <看更多>