SShortSingh.
Back to feed

Why 0.1 + 0.2 Does Not Equal 0.3 in Most Programming Languages

0
·1 views

Most programming languages return 0.30000000000000004 for 0.1 + 0.2 because they use IEEE 754 binary floating-point, which cannot store decimals like 0.1 exactly. The stored values are slightly off, and these tiny errors can accumulate, producing wrong results in calculations such as currency totals. For example, multiplying 4.35 by 100 yields 434.99999999999994, so truncating it to an integer loses a cent. The article recommends comparing floats with tolerance (such as math.isclose), storing money as integer cents, and building Decimal values from strings. It also notes that Python 3.12 and later use compensated summation in sum() to reduce rounding errors.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Guide Shows How to Scrape LinkedIn Job Postings Without Logging In

A developer tutorial explains how to collect LinkedIn job listings using public pages that are visible to logged-out visitors. It describes two sources: a paginated search endpoint that returns job cards with IDs, titles, companies, locations and posting dates, and individual job pages that include descriptions, criteria, applicant counts and sometimes salary. The author notes that some fields, such as external apply links, workplace type, job function and the poster, are only available to signed-in users or must be inferred from the text. The piece includes Python code using requests and BeautifulSoup, and the author discloses that they maintain a commercial LinkedIn jobs scraper on Apify. The approach was tested in November 2026.

0
ProgrammingDEV Community ·

dol8: A Python CI/CD gate that blocks pull requests with energy regressions

dol8 is an open-source CI/CD gate that stops pull requests from increasing a Python program's CPU energy use. It reads real RAPL energy measurements on bare-metal Linux and falls back to perf counters on virtual machines or CPU-time estimates on Windows. Its measurement core is written in Rust via PyO3, keeping the measurement separate from Python's own overhead, while Python handles statistics and orchestration. Every result is labeled with its measurement source and confidence, the gate avoids failing builds on noisy data, and comparisons across measurement types are blocked. The project is at v0.4-alpha under the MIT license, validated on AMD Ryzen, with Intel validation still in progress.

0
ProgrammingDEV Community ·

Kaze: A Go Terminal Client That Searches Multiple Sources and Drives mpv Over IPC

After repeated gaps in ani-cli, the developer built Kaze v4.3.0, a single static Go binary for Windows, macOS, and Linux that searches several sources at once. Queries run concurrently under a 3-second timeout, with results streamed into a Bubble Tea UI, and AUTO mode switches to another mirror when a CDN fails. The app controls mpv through IPC, using Unix sockets on Linux and macOS and Windows Named Pipes on Windows, which avoids shell injection and enables live shader swaps and skip-chapter controls. A security audit also removed an unsafe mpv TLS-bypass flag and added IP checks at dial time to block SSRF and DNS rebinding attacks.

0
ProgrammingDEV Community ·

A Single Line Made a Python Queue 2,500 Times Slower: Why list.pop(0) Is Costly

A developer found that processing one million tasks from a Python list took about 58 seconds because each pop(0) call shifted every remaining element. Replacing the list with collections.deque and using popleft() cut the runtime to roughly 0.023 seconds on Python 3.15.2 on an Apple M4. The article explains that list removal and insertion at the front cost O(n), so total work grows quadratically as data increases. It also covers related slowdowns, such as repeatedly building lists with out = out + [x] and string concatenation with += when the string has extra references. The author recommends deque for queues, append or += for lists, and str.join for building strings.