หนึ่งในปัญหาคลาสิก เวลาเขียนโปรแกรมที่ทุกคนต้องเจอเลย
ก็คือการบวกลบเลขทศนิยมในภาษาโปรแกรม ของบางภาษา นี้แหละ
เช่น 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 those classic problems, programming that everyone needs to encounter.
It's positive to delete decimal numbers in some language.
เช่น JavaScript, Python, Perl, C#, C, C++, Java, PHP, Fortran
(and many other languages not mentioned)
.
Many times it can be crazy like
👉 0.1 + 0.2 is not equal to 0.3
But I got to be 0.30000000000000004
.
👉 or 0.1 plus 10 times. It's not 1
But I got to be 0.9999999999999999
.
The Programmer found this.
It's like boxing. I got hit by a punch. I'm confused in dong code.
:
:
But it's not that it's crazy every time like
0.5 + 0.5 = 1 (exactly correct)
0.2 + 0.3 = 0.5 (accidentally not crazy)
.
For Case 0.2 and 0.3 it was cut down.
0.2000000000000000111022302462515654042363166809082031250
With the.
0.2999999999999999888977697537484345957636833190917968750
When we are positive, we get 0.5 fit like fluke which you shouldn't be able to do.
(I can watch the exam. 0.2 + 0.3 == 0.5 get the value to be true)
:
:
The cause is like this
Because computers only know the base number 2
Even if we write code, use base number 10
In the end, when the code is run, it will become base number 2 anyway.
.
😨 and it's bad luck to visit the programmer.
Because time to convert base number 10 to base number 2
In some cases, it's converted and I get the number that I don't end.
So it makes the decimal picking wrong.
.
For the decimal number 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 keeping it as a decimal number, base two.
According to IEEE-754 floating point standards.
0.0001100110011001100110011001100110011001100110011...
It's a decimal. I don't finish in the second base picture.... this is what the computer can see.
.
When the computer converts back to decimal so that the world can read and understand.
In the picture of base 10 will be.
0.1000000000000000055511151231257827021181583404541015625
But the computer will cut to only 0.1 (so people see this)
:
🤔 this kind of being
Sure. It makes cuddle times to calculate numbers.
- the more jobs need detailed answers such as banking work, there will be problems, etc.
- or time to use in comparison terms. If, while etc. There may be cuddle baht.
.
😀 but don't worry. in many languages, there will be a solution to this problem.
Prevent calculation of numbers from inaccurate such as
- in Java, there will be bigdecimal class to delete multiplication for decimal numbers especially.
- in python, there will be similar classes such as decimal.
- Javascript may use a lot of libraries to choose from.
https://github.com/MikeMcl/decimal.js/
https://github.com/MikeMcl/bignumber.js/
https://github.com/MikeMcl/big.js/
- the rest of the other languages, try to study it yourself.
.
.
It's important to delete multiplication, divide the decimal numbers. It's important that you should not overlook.
Personally, I have met a burp here.
At Project Level, bank level has been missed.
Finally, I have to sit and fix many lines of code.
Wasting time to sit and chase the test again.
.
Note that I see the comments. I wonder if
Php and c #survive the same fate?
I said I wouldn't survive.
.
// check out the code C Sample #
Console.WriteLine( ((0.1+0.2) == 0.3)); // False
Console.WriteLine( ((0.1+0.2) == 0.30000000000000004)); // True
// check out the sample code php
echo number_format(0.1+0.2 , 17);
.
++++++
Written by Thai Programmer Thai programmer
Read about IEEE-754 floating point at
https://th.wikipedia.org/wiki/จำนวนจุดลอยตัวTranslated
同時也有1部Youtube影片,追蹤數超過36萬的網紅Tiger Muay Thai and MMA Training Camp, Phuket, Thailand,也在其Youtube影片中提到,Peter Davis http://en.wikipedia.org/wiki/Peter_Davis_(actor) seemingly does it all. He is an actor, model, and ONE FC contracted fighter hailing from ...
「boxing wiki」的推薦目錄:
boxing wiki 在 環球膠報 Facebook 八卦
【超經典膠界大事!傳說中嘅9秒100拳】修道船大戰 Nobody-here
《修道船事件》
香港奇聞,自稱九秒內可打出一百拳,六年內實戰二千次,創立真武道的二十多歲年青武術家鄺啟源,網名修道船 ,在華山論劍功夫網聚京士柏聚會之後,揚言勁人NoBody-Here有腳無手("有手無腳"是誤傳),又稱NoBody-Here及當日聚會中人的武術不夠實戰,敵不過自己,經過一輪規則談判後接受NoBody-Here挑戰,相約於2003年01月25日旺角花園街市政大樓十樓活動室比試,以證明修道船一番驚人偉論屬實. 對戰開始約廿多秒,NoBody-Here以站立式斷頭檯頸鎖秒殺修道船,迫使修道船舉手投降,其後不足兩分鐘被高腿完全擊倒,隨後整個比武過程的片段於網絡各討論區大肆張貼. 當年尚未有Youtube,但此片滲透度極高,連一些偏僻地區的論壇也有片段下載,直到現在仍有在坊間流傳. 而此片段現身後不斷有人用「鐵達尼號」相片譏諷修道船.
事後修道船在他個人網上日記(現已關閉)仍稱自己有能力擊倒NoBody-Here,只不過當時手下留情而已,其後潛水消失成為潛水艇...
此戰另有別名沈船之戰
以下為修道船之前的日記記錄
大名: 船兒 電郵: bobo@hotmail.com 說: 我以前係打街戰出生既........ 冇錯,我而家既武功極差,但我唔在乎!因為我既心態唔同左.但我以前既對手既武功確實係no body之上,所以我唔覺得佢好勁. >>2003年08月11日 19:57:42
大名: 船兒 電郵: bobo@hotmail.com 說: 我係果次比武唔出手既理由我已經講過好多次,我亦唔打算再講.我既實力去到邊唔需要向任何人證明,眼看的未必是真!你有冇見過係水中被陽光折射既筷子?我以前單手打贏一個體重重過我45磅既柔道仔,當時有超過30人在場見證,你又在唔在場?我以前打贏過跆拳道高手同合氣道師傅,當時你又在唔在場?我以前個徒弟一個打贏6個人,而我以前武功最巔峰時既實力遠在佢之上,你又在唔在場?? >>2003年08月11日 19:53:15
大名: 船兒 電郵: bobo@hotmail.com 說: 你到底係邊個?果次我根本冇出過手.我曾經跟過一個年過50,識得超過二十種武術,浸淫詠春拳超過10年既詠春助教,佢既實力同玄空相比仲有一段距離,玄空既真正實力唔簡單架!! 我當然冇後悔跟過佢啦!! >>2003年08月08日 19:51:06
對戰規則
早期NoBody-Here只提出"30分鐘x1回合冇休息"一個要求,其餘任由修道船定下以下規則,以免修道船有"合理戰敗"的藉口:
1/雙方必需戴上護甲、頭盔、護腿、boxing拳套
2/不能用拳擊打對方頭部
3/沒有地戰,其中一方倒地要即時分開
4/不能使用關節技
此規則一出後,遭一眾華山論劍網友譏笑為"比香港武術聯會散手比賽更差勁的「機械人大戰」規則",於是再作出修改,成為最後的比賽規則:
1/可自由選擇戴或不戴護甲、頭盔、護腿、任何拳套
2/攻擊對方頭部時需要留力
3/可地戰
4/可使用關節技
5/2分鐘x1回合
http://evchk.wikia.com/wiki/%E4%BF%AE%E9%81%93%E8%88%B9%E4%BA%8B%E4%BB%B6
真正9秒100拳 (喵喵大戰)
https://www.facebook.com/photo.php?v=637133519685725
boxing wiki 在 โปรแกรมเมอร์ไทย Thai programmer Facebook 八卦
หนึ่งในปัญหาคลาสิก เวลาเขียนโปรแกรมที่ทุกคนต้องเจอเลย
ก็คือการบวกลบเลขทศนิยมในภาษาโปรแกรม ของบางภาษา นี้แหละ
เช่น 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
boxing wiki 在 Tiger Muay Thai and MMA Training Camp, Phuket, Thailand Youtube 的評價
Peter Davis http://en.wikipedia.org/wiki/Peter_Davis_(actor) seemingly does it all. He is an actor, model, and ONE FC contracted fighter hailing from Malaysia.
He was the winner of the Malaysian version of "Next Top Model". Peter is an avid martial artist. He is the co-host of MIMMA (MalaysianInvasion.com), an Ultimate fighter style reality tv show which recently did some filming at Tiger. Peter trained here alongside the MIMMA contestants for two weeks. It was a real pleasure to have Peter at camp. Despite his fame he is humble and down to earth. We hope to see him back soon.
Video by: Stuart Cooper
http://www.tigermuaythai.com/
http://www.fightingthai.com/

boxing wiki 在 WHAT WIKIPEDIA ISN'T TELLING YOU ABOUT BOXING NEWS 的八卦
WHAT WIKIPEDIA ISN'T TELLING YOU ABOUT BOXING NEWS. ... Devin Haney | Top Rank Boxing. Seconds Out. Seconds Out. ... <看更多>