หนึ่งในปัญหาคลาสิก เวลาเขียนโปรแกรมที่ทุกคนต้องเจอเลย
ก็คือการบวกลบเลขทศนิยมในภาษาโปรแกรม ของบางภาษา นี้แหละ
เช่น 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
「php require」的推薦目錄:
- 關於php require 在 โปรแกรมเมอร์ไทย Thai programmer Facebook 的最佳貼文
- 關於php require 在 美國在台協會 AIT Facebook 的精選貼文
- 關於php require 在 Eric's English Lounge Facebook 的精選貼文
- 關於php require 在 include和require 的区别?Include和include_once又有什么 ... 的評價
- 關於php require 在 Include and Require - Full PHP 8 Tutorial - YouTube 的評價
- 關於php require 在 php `require()` a file that has another `require()` - Stack Overflow 的評價
- 關於php require 在 using `require` in my functions.php breaks the site 的評價
- 關於php require 在 require/include · PHP技能树 的評價
php require 在 美國在台協會 AIT Facebook 的精選貼文
二月六日晚間11:50左右,花蓮東北方18公里處發生芮氏規模6.0地震。部分路段及橋樑受到影響。請參考中央社網頁 http://focustaiwan.tw/ 或ICRT https://www.icrt.com.tw/info_list01.php… 查詢最新的中英文訊息。
我們建議您直接與家人朋友聯繫,向他們報平安。若您有使用社群媒體,請務必更新您的狀態。如遇緊急醫療或消防事故,請撥 119。如您有緊急狀況需要警察協助,請撥 110。
提醒在台灣的美國公民,如果您尚未註冊登記 Smart Traveler Enrollment Program (STEP),請至網站 https://step.state.gov/step 登記。您將會不定期收到有關在台灣的旅遊暨安全訊息 ,並使AIT在緊急狀況時更容易聯繫到您。
有關美國公民的緊急事件,請與美國在台協會美國公民服務科聯繫 (TaipeiACS@state.gov)。
美國公民如需確認親友家人在台灣是否平安,可以打這支24小時中英文專線:011-886-0800-085-095。
On February 6, 11:50pm, a 6.0 magnitude earthquake – centered 18 kilometers northeast of Hualien, off the eastern coast – struck Taiwan. Some bridges and highways in the area are affected. For the latest English-language updates, please check the CNA Focus Taiwan news page http://focustaiwan.tw/ and ICRT https://www.icrt.com.tw/info_list01.php….
We strongly encourage everyone in Taiwan to reach out to family and friends directly to assure them you are okay. If you use social media, be sure to update your status. Only if you require emergency medical assistance or need to report a fire, dial 119. If you need emergency police assistance, dial 110.
All U.S. citizens in Taiwan are encouraged to enroll online in the Smart Traveler Enrollment Program (STEP): https://step.state.gov/step. Registering gives U.S citizens access to updated information on travel and security within Taiwan and makes it easier for AIT to contact citizens in case of emergency.
For any emergencies involving U.S. citizens, please contact the American Citizen Services Unit of the American Institute in Taiwan (TaipeiACS@state.gov).
Please monitor the American Institute in Taiwan's website (https://www.ait.org.tw/) and consult the Country Specific Information for Taiwan, on Travel.State.Gov website (https://travel.state.gov/…/International-Travel…/Taiwan.html).
ADDITION: Americans seeking information about their loved ones in Taiwan can call the 24/7 crisis information phone number 011-886-0800-085-095. This is a service available in English and Chinese.
php require 在 Eric's English Lounge Facebook 的精選貼文
[時事英文] 同學,讓我們來關注巴黎氣候峰會的結論! 這高峰會關乎人類未來的氣候變化,而其中的重要的時事英文字詞,更是值得我們學習!
★★★★★★★★★★★★
時事英文詞彙和搭配詞:
think tank 智庫
call for action against…呼籲對....採取行動
non-binding commitments 非約束性的承諾
legally binding 具法律約束力的
lack of enforcement mechanisms 缺乏強制執行機制
ratify the agreement 批准該協定
pursue efforts to 為…繼續努力
summit 尖峰、最高級會議
agreement 協定,協議
accord (國家之間的)協議;條約[C][(+with/on)]
protocol 議定書;協議;草案;(尤指外交的)禮節
adopted version 通過的版本
emerging economies 新興經濟體
CO2 emissions 二氧化碳排放量
greenhouse gases 溫室氣體
climate change 氣候變化
a rise in sea levels 海平面上升
continue to rise at their current rate 以現在的速率持續上升
reduce emissions 減少排放
curb emissions 控制排放
decarbonize electricity production 脫碳電力生產
generate electricity 發電
★★★★★★★★★★★★
accord, treaty, protocol, agreement 的用法
accord 指涉的是同意、和諧、妥協、諒解的"狀態",通常是指國家間的共識性協議。accord雖然屬於國際文件,但因未創設國際法上的權利義務,大多不具備國際法拘束力。An accord is a formal agreement between countries or groups (e.g. the Helsinki accord on human rights).
treaty 採廣義的定義是指國家之間經外交談判後依照國際法簽訂的正式條約。A treaty is a formal written agreement between two or more countries or governments. (e.g. The peace treaty ends nearly four years of violence.)
protocol 指的是一個議定書、協議、草案,通常用來表示一個公約的輔助文件,一個公約的附屬文件,或一個獨立條約的名稱。A protocol can mean the following things: A) an international agreement between two or more countries (e.g. the Montreal protocol on the protection of the ozone layer); B) a written record of a formal or international agreement, or an early form of an agreement
agreement 是協議最常用廣泛的叫法。跟treaty的定義差不多,指國際法主體間針對特定事項的條約,具有國際法效力。An agreement is an official document that people sign to show that they have agreed to something. It can be a pact, convention, or treaty between nations, sub-national entities, organizations, corporations, or other entities or persons.
★★★★★★★★★★★★
時事英文全文:
The 2015 United Nations Climate Change Conference, Conference of Parties 21 (COP 21), was held in Paris, France, from 30 November to 12 December 2015.
One hundred and ninety-six parties attended the conference and negotiated the Paris Agreement, a global agreement on the reduction of climate change. The overall goal of the agreement is to limit global warming to less than 2 degrees Celsius (°C) compared to pre-industrial levels. The agreement calls for zero net man-made greenhouse gas emissions to be reached during the second half of the 21st century. In the adopted version of the Paris Agreement, the parties will also pursue efforts to limit the temperature increase to 1.5 °C. According to some scientists, the 1.5 °C goal will require zero emissions sometime between 2030 and 2050.
The agreement will become legally binding if signed by at least 55 countries which together represent at least 55 percent of global greenhouse emissions. The parties will need to sign the agreement in New York between in April 2017, and also adopt it within their own legal systems.
Sources:
http://namasnthu.blogspot.tw/2011/12/copkp-platform.html
http://www.ldoceonline.com/dictionary/
http://unfccc.int/meetings/paris_nov_2015/meeting/8926.php
https://en.wikipedia.org/wiki/United_Nations_Climate_Change_conference
★★★★★★★★★★★★
時事英文: http://goo.gl/3EnOO6
環保詞彙: http://goo.gl/z7w7UO
★★★★★★★★★★★★
php require 在 Include and Require - Full PHP 8 Tutorial - YouTube 的推薦與評價
In this PHP tutorial, you will learn how to include PHP files with some actual practical examples. You will learn how to use output ... ... <看更多>
php require 在 include和require 的区别?Include和include_once又有什么 ... 的推薦與評價
对这两者来说,不管使用哪个语句来包含文件,解析程序都将退出php模式并在目标文件的开头进入HTML模式。这意味着目标文件中的所有应该作为php脚本执行的代码都必须被包含在 ... ... <看更多>