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還有很多好用的功能,可以到它官方的網站閱讀一些文章,基本上所有的數學運算應該都可以透過它來玩完成。
The Basel Committee recognizes that operational danger is a term that has a wide range of meanings and therefore, for inner purposes, banks are permitted to adopt their own definitions of operational danger, provided that the minimum elements in the Committee’s definition are included.
https://fastpillsformen.com/# Viagra Tablet price
Channel your internal Bey with a pair of trendy silver pants seen throughout TikTok, and coordinate with mates who shall be rocking out to “Break My Soul” in a silver fringe fit.
Аттестат 11 класса купить официально с упрощенным обучением в Москве
bb gate – breakingbad, breaking bad forum
http://fastpillseasy.com/# pills for ed online
Оцените огромный выбор игр, от классики до современных новинок, в 1xBet. Узнайте больше здесь: https://1xbet-zerkalo9.top/
http://maxpillsformen.com/# Cialis without a doctor prescription
https://maxpillsformen.com/# Generic Cialis price
https://maxpillsformen.com/# Cialis over the counter
вызвать наркологическую помощь http://justforum.bestforums.org/viewtopic.php?f=26&t=4785/ .
http://fastpillsformen.com/# Generic Viagra online
наркологическая скорая бесплатная наркологическая скорая бесплатная .
Buy Cialis online: MaxPillsForMen – Cialis over the counter
best ed pills online ed med online where can i get ed pills
Tadalafil Tablet: Generic Cialis without a doctor prescription – buy cialis pill
cheap ed cheap cialis cheap ed drugs
https://maxpillsformen.com/# Buy Tadalafil 10mg
best price for viagra 100mg cheap viagra generic sildenafil
неотложная наркологическая помощь неотложная наркологическая помощь .
срочная наркологическая помощь в москве http://www.belbeer.borda.ru/?1-6-0-00000783-000-0-0-1730876081 .
вызвать наркологическую помощь вызвать наркологическую помощь .
Optimize Your Crypto Trading with TraderJoeXyz
In the dynamic world of cryptocurrency, having the right tools at your disposal can make all the difference. Enter TraderJoeXyz, a cutting-edge platform designed to maximize your trading potential. By leveraging TraderJoeXyz, traders gain access to a multitude of features that cater to both beginners and seasoned professionals. Let’s explore how TraderJoeXyz can enhance your crypto trading journey.
[url=https://traderjonexzy.com]trader joe xyz[/url]
Features of TraderJoeXyz
The TraderJoeXyz platform is packed with robust features aimed at simplifying and optimizing cryptocurrency trading:
Advanced Trading Interface: The user-friendly interface is equipped with powerful tools to execute trades efficiently.
Comprehensive Analytics: Gain insights with detailed analytics and customizable charts for better decision-making.
Secure Wallet Integration: Seamlessly manage your crypto assets with integrated security measures.
Automated Trading Bots: Use AI-driven bots to enhance your trading strategy with automated buy and sell actions.
Benefits of Using TraderJoeXyz
Here are some of the benefits you can expect when using TraderJoeXyz:
Increased Efficiency: With streamlined processes and intuitive design, traders can execute trades faster than ever.
Greater Profit Potential: Advanced tools and analytics help you identify profitable trading opportunities.
Reduced Risk: Implementing secure wallet integrations and automated bots can help mitigate risks associated with trading.
Enhanced Trading Experience: A seamless interface paired with powerful tools ensures an exceptional trading journey.
Join the TraderJoeXyz Community
TraderJoeXyz is more than just a platform; it’s a community of crypto enthusiasts and experts. Joining this community provides access to shared knowledge, support, and collaboration opportunities. To become part of this thriving ecosystem, simply and start exploring the world of crypto trading today.
By choosing TraderJoeXyz, you’re not just trading—you’re transforming your crypto experience. Get started and make the most of your cryptocurrency assets.
Understanding the Aave Protocol
The Aave Protocol is revolutionizing the decentralized finance (DeFi) space with its unique approach to crypto lending and borrowing. Whether you’re a seasoned investor or new to the world of cryptocurrencies, Aave offers a robust platform for managing your digital assets.
[url=https://www-aave.com]aave lending[/url]
What is Aave?
Aave, which means ‘ghost’ in Finnish, is a non-custodial liquidity protocol. It allows users to earn interest on deposits and borrow assets. Aave is known for its wide range of supported cryptocurrencies and features that enhance the security and flexibility of crypto transactions.
Key Features of Aave Protocol
Flash Loans: Aave introduced the concept of flash loans, which are borrowed and repaid within a single transaction. This feature is useful for arbitrage opportunities and collateral swaps.
Security: Aave is audited by leading blockchain security firms, ensuring the safety of user funds.
Rate Switching: Users can switch between stable and variable interest rates, offering flexibility based on market conditions.
Wide Asset Support: Aave supports multiple cryptocurrencies including Ethereum (ETH), DAI, and more.
How to Get Started with Aave
Getting started with Aave is straightforward:
Set Up a Wallet: Use a compatible crypto wallet like MetaMask.
Connect to Aave: Visit the Aave website and connect your wallet.
Deposit Crypto: Choose from supported cryptocurrencies to deposit into the Aave Protocol.
Start Earning or Borrowing: Once your crypto is deposited, you can start earning interest or borrowing assets instantly.
Advantages of Using Aave
There are several reasons why Aave stands out in the world of DeFi:
Non-Custodial: Users maintain control over their funds.
Highly Secure: Regular audits and community governance enhance security.
Innovative Products: Pioneering features like flash loans provide unparalleled opportunities.
In conclusion, the Aave Protocol offers a revolutionary platform for anyone looking to explore the potential of decentralized finance. Whether you’re earning interest or borrowing assets, Aave provides a secure and flexible experience.
Как оказалось, купить диплом кандидата наук не так уж и сложно
Introducing Velodrome Finance: Maximize Your Crypto Yields
In the rapidly evolving world of decentralized finance (DeFi), Velodrome Finance emerges as a robust platform for enthusiasts looking to enhance their crypto yield returns. This guide will walk you through the essentials of Velodrome Finance and how you can benefit from its features.
[url=https://web-velodirome.com]velodrome finance[/url]
Why Choose Velodrome Finance?
Velodrome Finance stands out as a comprehensive DeFi protocol designed specifically for liquidity providers. Its innovative approach focuses on maximizing rewards while maintaining efficient and secure trading mechanisms. Here’s why it’s capturing the attention of the DeFi community:
Efficient Token Swaps: Velodrome offers seamless and cost-effective token swapping capabilities.
Liquidity Pools: Participants can provide liquidity to various pools, optimizing their earning potential.
Yield Optimization: With advanced strategies, Velodrome helps users achieve superior returns on their investments.
Secure Protocol: Security is a top priority, and Velodrome utilizes cutting-edge technology to protect user assets.
Getting Started with Velodrome
Embarking on your journey with Velodrome Finance is straightforward. Here’s a step-by-step guide to help you dive into the platform:
Create a Wallet: To engage with Velodrome, you first need a compatible crypto wallet.
Connect Your Wallet: Visit and securely link your crypto wallet.
Explore Liquidity Pools: Browse through available pools and decide where to allocate your assets for optimal returns.
Stake and Earn: Once you’ve funded a pool, begin staking and watch your earnings grow as you benefit from trading fees and incentives.
Community and Support
Velodrome Finance boasts a vibrant community ready to assist users at any step. Whether you’re a seasoned DeFi user or a newcomer, you can find guidance and support from community forums and dedicated customer service.
Conclusion
With its focus on maximizing crypto yield, Velodrome Finance is a compelling choice for anyone looking to delve deeper into the DeFi space. From efficient token swaps to robust security measures, it offers a complete ecosystem for those eager to optimize their returns. Visit the official site and start your journey towards enhanced financial growth.
Maximizing Profits with 1inch Exchange
In the fast-paced world of cryptocurrency, every second counts. 1inch Exchange offers a powerful solution for traders looking to optimize their crypto transactions. By aggregating the best deals across various decentralized exchanges (DEXs), 1inch ensures users get the most value for their trades.
[url=https://www-1nch.com]1inch wallet[/url]
What is 1inch Exchange?
1inch Exchange is a decentralized exchange aggregator. It searches multiple DEXs to find the most efficient path for your trade, thus minimizing costs and maximizing returns. By splitting your transaction into parts and executing them across different platforms, 1inch achieves the best possible market rates.
Key Benefits of Using 1inch Exchange
Cost Efficiency: By seeking the best rates across multiple platforms, 1inch saves you money on each transaction.
Security: Operating on a decentralized network means that your assets are secure and you maintain control of your keys.
Liquidity: Access a vast pool of liquidity across numerous exchanges, ensuring that your trades are executed quickly and with minimal slippage.
How Does 1inch Work?
1inch deploys a sophisticated algorithm that splits your trade across multiple exchanges. This process uses smart contracts to ensure every part of the transaction is executed seamlessly and securely. 1inch’s pathfinder algorithm analyzes multiple liquidity sources within seconds to find the best exchange rates for your trade.
Getting Started with 1inch
Getting started with 1inch Exchange is easy. Follow these simple steps:
Visit the 1inch website and connect your digital wallet.
Select the token you wish to trade and the token you want to receive.
1inch displays the best available rates and allows you to execute the trade directly from the platform.
Conclusion
1inch Exchange is an invaluable tool for cryptocurrency traders looking to enhance their trading efficiency. By securing the best rates and offering robust security measures, 1inch stands out as a top choice for optimizing crypto swaps. Explore 1inch today and take your trading to the next level.
скорая наркологическая помощь на дому в москве [url=www.4dkp.forum24.ru/?1-18-0-00002760-000-0-0-1730876849]скорая наркологическая помощь на дому в москве[/url] .
срочная наркологическая помощь срочная наркологическая помощь .
срочная наркологическая помощь срочная наркологическая помощь .