Javascript的數學難題
首先先考大家幾個數學題,如果這幾題的答案你都能順利的回答,那恭喜你!你可以安心地閱讀這篇文章,你一定能看懂!
題目ㄧ. 0.1 + 0.2 = ?
題目二. 1 – 0.9 = ?
題目三. 0.69 x 10 = ?
相信上面三個簡單的十進位數學運算,你的答案應該會跟我一樣吧?
第一題. 0.3
第二題. 0.1
第三題. 6.9
上面應該是無庸置疑的答案,但是如果我們把這三個題目丟給Javascript去運算之後,得到的結果會變成這樣:
第一題. 0.1 + 0.2 = 0.30000000000000004
第二題. 1 – 0.9 = 0.09999999999999998
第三題. 0.69 x 10 = 6.8999999999999995
什麼!?Javascript連這種小學生的算術都會算錯嗎?其實是有原因的,Javascript所有數字都是以64位浮點數形式儲存,也就是所有的數字都會變轉換成二進位。
上面題目中特別有提到這是3個十進位的數學運算,這也就是這個題目的重點,如果今天的題目改成:10 / 3 = 3.3333333333333334 也許你就能比較明白了,也就是說在十進位中要表達 1 / 3 和二進位中表達 1 / 10 一樣困難。
但其實在我們設計網頁的過程中,使用者是沒有二進位觀念的,只在乎運算出來的數值是否符合十進位的結果,這個時候我們可以利用一些技巧來達成正確的結果。
方法1 . 將小數變成整數後再進行運算
第一題. ((0.1 x 10) + (0.2 x 10)) / 10 = 0.3
第二題. ((1 x 10) – (0.9 x 10)) / 10 = 0.1
第三題. ((0.69 x 100) x 10 / 100) = 6.9
雖說以上的方法能夠順利完成任務,但若運算的數值是動態(Dynamic)的,乘數的部分就比較難下,所以我們可以採用第二種方法。
方法2. Math.js(https://mathjs.org/)
透過網站上提供的js,或是透過CDN(https://cdnjs.com/libraries/mathjs)來引入後,透過以下的方法,就可以正確計算出上面三個題目的結果,其中14指的是精準度,通常已經很夠用了。
第一題. math.format(0.1 + 0.2 , 14) = 0.3
第一題. math.format(1 – 0.9 , 14) = 0.1
第一題. math.format(0.69 x 10 , 14) = 6.9
其實Math.js還有很多好用的功能,可以到它官方的網站閱讀一些文章,基本上所有的數學運算應該都可以透過它來玩完成。
пропуск для грузового транспорта в москву пропуск для грузового транспорта в москву .
пропуска в москву для грузовиков пропуска в москву для грузовиков .
darknet sites dark web sites links
https://cytpharm.shop/# buy cytotec online
Kamagra tablets
where to buy prednisone in australia: prednisone pill 20 mg – PredPharm
http://predpharm.com/# Pred Pharm
prednisone without prescription
cali weed for sale in prague https://shop-cannabis-prague.com
A brief history of sunglasses, from Ancient Rome to Hollywood
kraken darknet
Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?
The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.
Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.
When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.
We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.
In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.
priligy: dap pharm – priligy
https://semapharm24.shop/# semaglutide tablets
prednisone price south africa
dark web market links https://github.com/darknetmarkets24/darknet-markets deep web drug links
пропуск в москву для грузовиков пропуск в москву для грузовиков .
darknet sites dark web access drug markets onion
A brief history of sunglasses, from Ancient Rome to Hollywood
kraken marketplace
Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?
The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.
Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.
When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.
We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.
In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.
dark web sites https://github.com/darknetmarkets24/darknet-markets drug markets onion
A brief history of sunglasses, from Ancient Rome to Hollywood
кракен ссылка
Sunglasses, or dark glasses, have always guarded against strong sunlight, but is there more to “shades” than we think?
The pupils of our eyes are delicate and react immediately to strong lights. Protecting them against light — even the brilliance reflected off snow — is important for everyone. Himalayan mountaineers wear goggles for this exact purpose.
Protection is partly the function of sunglasses. But dark or colored lens glasses have become fashion accessories and personal signature items. Think of the vast and famous collector of sunglasses Elton John, with his pink lensed heart-shaped extravaganzas and many others.
When did this interest in protecting the eyes begin, and at what point did dark glasses become a social statement as well as physical protection?
The Roman Emperor Nero is reported as holding polished gemstones to his eyes for sun protection as he watched fighting gladiators.
We know Canadian far north Copper Inuit and Alaskan Yupik wore snow goggles of many kinds made of antlers or whalebone and with tiny horizontal slits. Wearers looked through these and they were protected against the snow’s brilliant light when hunting. At the same time the very narrow eye holes helped them to focus on their prey.
In 12th-century China, judges wore sunglasses with smoked quartz lenses to hide their facial expressions — perhaps to retain their dignity or not convey emotions.
Thieves blow up museum door and steal ancient artifacts, including 2,500-year-old gold helmet
гей порно большой
Thieves have stolen four ancient artifacts, including an approximately 2,500-year-old gold helmet, after using explosives to break into a museum in the Netherlands.
The daring heist took place at Drents Museum in Assen during the early hours of Saturday morning, according to Dutch police, who said they received a report of an explosion at 3:45 a.m. local time.
CCTV footage released by police shows the suspects opening an exterior door before a blast sends sparks and smoke into the air. The thieves made off with three gold bracelets, dating from around 50 BC, as well as the 5th-century BC Helmet of Cotofenesti, a historically important artifact on loan from the National History Museum of Romania in Bucharest.
The items were part of an exhibition about the Dacians, an ancient society that occupied much of present-day Romania before being conquered by the Romans. On show since July, “Dacia: Empire of Gold and Silver” featured treasures borrowed from institutions across Romania.
In a press release on its website, Drents Museum described the Helmet of Cotofenesti — which was discovered in a Romanian village almost a century ago — as a “masterpiece.” Its design features mythological scenes and a pair of eyes, located above the wearers’, that were thought to deter enemies during battle while protecting against the “evil eye.”
The exhibition was set to conclude Sunday, though Drents Museum remained closed through the weekend due to the robbery. Its premises were damaged by the explosion, though no injuries were reported, the museum said.
Dutch police announced that they are working with global police agency Interpol and had, as of Sunday, received more than 50 tip-offs. Investigators are currently looking for information about a gray car that was stolen from the nearby city of Alkmaar earlier in the week and was discovered around four miles from the crime scene, on fire, shortly after the overnight heist.
dark market 2022 dark web markets
dark market 2022 dark market onion
drug markets onion tor marketplace
darknet market lists dark web market list black internet
darknet websites tor darknet darkmarket url
sildenafil oral jelly 100mg kamagra: Kamagra tablets – buy Kamagra
https://semapharm24.com/# buy semaglutide
prednisone buy without prescription
buy dapoxetine online buy dapoxetine online cheap priligy
https://predpharm.shop/# buy prednisone online fast shipping
prednisone over the counter cost
Kama Pharm: Kama Pharm – Kamagra Oral Jelly
black internet dark websites dark market url
darkmarket link tor dark web
darknet market list https://github.com/darknetmarkets24/darknet-markets darknet drug store