<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ float - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ float - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 16 Jul 2026 19:50:16 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/float/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is a Floating-Point Arithmetic Problem? ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever worked with numbers like 1/3, where the result is 0.33333… and continues forever? As humans, we naturally round off such numbers, but have you ever wondered how computers handle them? In this article, you’ll explore how computers manage... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-a-floating-point-arithmetic-problem/</link>
                <guid isPermaLink="false">671a576a1d53cf0b6813a51c</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Maths ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ float ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Syeda Maham Fahim ]]>
                </dc:creator>
                <pubDate>Thu, 24 Oct 2024 14:19:22 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729588035734/9824633d-727a-49ce-9080-0fa3a7b18ed6.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever worked with numbers like 1/3, where the result is 0.33333… and continues forever? As humans, we naturally round off such numbers, but have you ever wondered how computers handle them?</p>
<p>In this article, you’ll explore how computers manage continuous values, including the concept of precision errors. We’ll examine the floating-point arithmetic problem, a universal issue that affects many programming languages. We’ll focus specifically on how JavaScript addresses this challenge.</p>
<p>Additionally, you’ll learn how binary operations work behind the scenes, the threshold at which JavaScript truncates numbers based on the IEEE 754 standard, and introduce <code>BigInt</code> as a solution for accurately handling larger numbers without precision loss.</p>
<p>First, let's consider an example. Can you guess the output of this operation?</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>);
</code></pre>
<p>You may think the answer is 0.3, right? But no, the actual output is:</p>
<pre><code class="lang-javascript">Output: <span class="hljs-number">0.30000000000000004</span>
</code></pre>
<p>You must be wondering why this is happening. Why so many extra zeros, and why does it end with a 4?</p>
<p>The answer is simple: the numbers 0.1 and 0.2 cannot be precisely represented in JavaScript (that is, "exactly" or "accurately.")</p>
<p>It sounds simple, right? But the explanation is a bit more complex.</p>
<p>So, what do you think—bug or feature?</p>
<p>Well, it’s not a bug. It’s actually a fundamental issue with how computers handle numbers, specifically floating-point numbers.</p>
<h2 id="heading-why-does-this-happen">Why Does This Happen?</h2>
<p>Let’s understand this with basic math.</p>
<p>The fraction 1/3 is represented in decimal by 0.33333... and it never ends. This means that 3 repeats itself infinitely. We can’t write it down exactly, so we approximate it to something like 0.333 or 0.333333 to save time and space.</p>
<p>Similarly, in a computer, we also have to approximate because 1/3 or 0.3333... would be a very large number and take up infinite space (which we don’t have).</p>
<p>This leads to what we call the floating-point arithmetic problem.</p>
<h2 id="heading-floating-point-arithmetic-problem">Floating-Point Arithmetic Problem</h2>
<p>In simple terms, floating-point numbers are numbers that cannot be written down exactly, so we approximate them. In a computer, this kind of approximation can lead to small precision errors, which we call the floating-point arithmetic problem.</p>
<h2 id="heading-binary-explanation">Binary Explanation</h2>
<p>Now that we've covered the simple explanation, let’s also understand this in binary terms. JavaScript handles everything in binary behind the scenes.</p>
<p>Binary is a number system that only uses two digits: 0 and 1.</p>
<h3 id="heading-why-cant-01-and-02-be-represented-exactly-in-binary">Why Can’t 0.1 and 0.2 Be Represented Exactly in Binary?</h3>
<p>The core issue is that not all decimal numbers can be perfectly represented as binary fractions.</p>
<p>Let’s take 0.1 as an example:</p>
<p>When you try to represent 0.1 in binary, you’ll find out that it can’t be expressed as a finite binary fraction. Instead, it becomes a repeating fraction, much like how 1/3 in decimal becomes 0.333..., repeating forever.</p>
<p>In binary, 0.1 becomes:</p>
<pre><code class="lang-plaintext">0.0001100110011001100110011001100110011... (repeating infinitely)
</code></pre>
<p>Since computers have limited memory, they can’t store this infinite sequence exactly. Instead, they have to cut off the number at some point, which introduces a small rounding error. This is why 0.1 in binary is only an approximation of the actual value.</p>
<p>Like 0.1, 0.2 can’t be exactly represented in binary. It becomes:</p>
<pre><code class="lang-plaintext">0.00110011001100110011001100110011... (repeating infinitely)
</code></pre>
<p>Again, the computer truncates (cutting off part of a number to fit a limit or remove extra digits) this infinite binary sequence, leading to a small error in representation.</p>
<p>So, what happens when we add 0.1 + 0.2? When you add 0.1 + 0.2 in JavaScript, the binary approximations for 0.1 and 0.2 are added together. But since both values are only approximations, the result is also an approximation.</p>
<p>Instead of getting exactly 0.3, you get something close to this:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>); <span class="hljs-comment">// Output: 0.30000000000000004</span>
</code></pre>
<p>This slight error occurs because neither 0.1 nor 0.2 can be represented exactly in binary, so the final result has a small rounding error.</p>
<h2 id="heading-how-does-javascript-truncate-the-number">How Does JavaScript Truncate the Number?</h2>
<p>Now, the question arises: how does JavaScript know when to truncate the value?</p>
<p><strong>( Truncation</strong> means cutting off or shortening a number by removing extra digits beyond a certain point. <strong>)</strong></p>
<p>There’s a maximum and minimum limit for it.</p>
<p>To handle this in the computer world, we have a standard that defines how floating-point numbers are stored and calculated.</p>
<h2 id="heading-ieee-754-standard">IEEE 754 Standard</h2>
<p>JavaScript uses the IEEE 754 standard to handle floating-point arithmetic.</p>
<p>The standard defines safe integer limits for the <code>Number</code> type in JavaScript without precision loss:</p>
<ul>
<li><p><strong>Maximum Safe Integer:</strong> 2^53 - 1 or 9007199254740991</p>
</li>
<li><p><strong>Minimum Safe Integer:</strong> -(2^53 - 1) or -9007199254740991</p>
</li>
</ul>
<p>Beyond these limits, JavaScript cannot accurately represent integers due to the way floating-point arithmetic works.</p>
<p>For this reason, JavaScript provides two constants to represent these limits:</p>
<ul>
<li><p><code>Number.MAX_SAFE_INTEGER</code></p>
</li>
<li><p><code>Number.MIN_SAFE_INTEGER</code></p>
</li>
</ul>
<h3 id="heading-what-if-i-need-a-bigger-number">What If I Need a Bigger Number?</h3>
<p>If you need to work with numbers larger than the Maximum Safe Integer (like those used in cryptography or finance), JavaScript has a solution: BigInt.</p>
<h3 id="heading-enter-bigint">Enter BigInt</h3>
<p><code>BigInt</code> is a built-in object that allows you to work with whole numbers beyond the safe integer limit. It enables you to represent numbers larger than 9007199254740991, so you don't need to worry about precision errors here!</p>
<p>To use <code>BigInt</code>, simply append an <code>n</code> to the end of an integer literal:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> bigNumber = <span class="hljs-number">1234567890123456789012345678901234567890n</span>;
</code></pre>
<p>Alternatively, you can use the <code>BigInt</code> constructor:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> bigNumber = BigInt(<span class="hljs-string">"1234567890123456789012345678901234567890"</span>);
</code></pre>
<h4 id="heading-operations-with-bigint">Operations with BigInt</h4>
<p>You can perform arithmetic operations with <code>BigInt</code>, like addition, subtraction, multiplication, and even exponentiation. However, there’s a catch: you can’t mix <code>BigInt</code> with regular <code>Number</code> types in arithmetic operations without explicitly converting between them.</p>
<p>For example, this won’t work:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> result = bigNumber + <span class="hljs-number">5</span>; <span class="hljs-comment">// Error: cannot mix BigInt and other types</span>
</code></pre>
<p>You would need to convert the <code>Number</code> to <code>BigInt</code> first:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> result = bigNumber + BigInt(<span class="hljs-number">5</span>); <span class="hljs-comment">// Now it works!</span>
</code></pre>
<h3 id="heading-where-do-we-use-bigint">Where Do We Use BigInt?</h3>
<p><code>BigInt</code> is particularly useful in areas requiring precision, such as:</p>
<ul>
<li><p>Cryptographic algorithms</p>
</li>
<li><p>Handling large datasets</p>
</li>
<li><p>Financial calculations requiring exactness</p>
</li>
</ul>
<h3 id="heading-in-summary">In Summary</h3>
<ul>
<li><p>The safe integer limit in JavaScript ensures accurate number representation for integers between -(2^53 - 1) and 2^53 - 1.</p>
</li>
<li><p>Precision errors occur due to floating-point arithmetic when handling certain numbers (like 0.1 + 0.2).</p>
</li>
<li><p>If you need numbers bigger than the safe limit, <code>BigInt</code> is your friend. But remember, mixing <code>BigInt</code> and <code>Number</code> types require explicit conversions.</p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Pandas round() Method – How To Round a Float in Pandas ]]>
                </title>
                <description>
                    <![CDATA[ You can use the Pandas library in Python to manipulate and analyze data. In most cases, it is used for manipulating and analyzing tabular data.  In this article, you'll learn how to use the Pandas round() method to round a float value to a specified ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-round-a-float-in-pandas/</link>
                <guid isPermaLink="false">66b0a2e55e73cf343a5cc012</guid>
                
                    <category>
                        <![CDATA[ float ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pandas ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ihechikara Abba ]]>
                </dc:creator>
                <pubDate>Mon, 13 Mar 2023 21:49:09 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/round-float-in-pandas.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You can use the Pandas library in Python to manipulate and analyze data. In most cases, it is used for manipulating and analyzing tabular data. </p>
<p>In this article, you'll learn how to use the Pandas <code>round()</code> method to round a float value to a specified number of decimal places. </p>
<p>We'll begin by looking the method's syntax, and then see some practical code applications. </p>
<h2 id="heading-pandas-round-method-example">Pandas <code>round()</code> Method Example</h2>
<p>Here's what the syntax for the <code>round()</code> method looks like:</p>
<pre><code class="lang-txt">DataFrame.round(decimals)
</code></pre>
<p>The <strong>decimals</strong> parameter represents the number of decimal places a number should be rounded to.</p>
<p>The number of decimal places to be returned is passed in as a parameter. <code>round(2)</code> return rounds a number to two decimal places. </p>
<p>Here's an example to demonstrate:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd

data = {<span class="hljs-string">'cost'</span>:[<span class="hljs-number">20.5550</span>, <span class="hljs-number">21.03535</span>, <span class="hljs-number">19.67373</span>, <span class="hljs-number">18.233233</span>]}

df = pd.DataFrame(data)

df[<span class="hljs-string">'rounded_cost'</span>] = df[<span class="hljs-string">'cost'</span>].round(<span class="hljs-number">2</span>)
print(df)
</code></pre>
<p>In the code above, we have a list of numbers that fall under the <code>cost</code> column. The column had these values: [20.5550, 21.03535, 19.67373, 18.233233]. </p>
<p>Using the <code>round()</code> method, we rounded the values to 2 decimal places: <code>df['cost'].round(2)</code>. </p>
<p>The return values were stored in a column called <code>rounded_cost</code>. </p>
<p>Here's the output of the code:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td></td><td>cost</td><td>rounded_cost</td></tr>
</thead>
<tbody>
<tr>
<td>0</td><td>20.555000</td><td>20.56</td></tr>
<tr>
<td>1</td><td>21.035350</td><td>21.04</td></tr>
<tr>
<td>2</td><td>19.673730</td><td>19.67</td></tr>
<tr>
<td>3</td><td>18.233233</td><td>18.23</td></tr>
</tbody>
</table>
</div><p>From the table above, you can see that the values in the <code>cost</code> column have been rounded to 2 decimal places in the <code>rounded_cost</code> columns. </p>
<h2 id="heading-summary">Summary</h2>
<p>In this article, we have learned about rounding float values with Pandas using the <code>round()</code> method. </p>
<p>We started looking at the syntax for the <code>round()</code> method looks like. We then saw an example using the method to round float values to a specified number of decimal places. </p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
