CharlesO
Rounding Error with Decimal Sum
Hi.
Please I’m getting this unusual rounding error when summing numbers using the Decimal Library
My result is off by 0.01
208,505,442,949.08 # Excel
208,505,442,949.09 # sum function with Decimal
Any suggestions please?
Thanks.
def sum() do
f = "C:/Downloads/MMR100.txt"
{:ok, b} = :file.read_file(f)
a = String.split(b,"\n")
c = Enum.count(a)
IO.puts c # 299,725
_sum(a, "0.00") |> Decimal.to_string(:normal)
# 208,505,442,949.08 - Excel
# 208,505,442,949.09 - sum
end
defp _sum([], acc), do: acc
defp _sum(["" | rest], acc), do: _sum(rest, Decimal.add("0", acc))
defp _sum([v | rest], acc), do: _sum(rest, Decimal.add("#{v}", acc))
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 10 of 11 Posts!
LostKobrakai
Afaik excel does use floats, so maybe it’s excel, which is wrong.
CharlesO
all my values are 2-decimal places
The Validator Program (some proprietor java program) also tells me the sum is wrong:
When i manually adjust the sum to that excel value and feed it to the validator .. it validates
NobbZ
Parsing and summing the list as float gives:
208505442949.07806, rounding this to 2 decimal places returns208505442949.08, so obviously Excel and your verification program do use floats and therefore are inaccurate by design…PS: I’ve heard there are still people using Excel to manage their money…
PPS: my iex session:
CharlesO
All the numbers in that list are 2-dp, how can you get
208505442949.07806???NobbZ
The magic of floats.
LostKobrakai
This exactly is the reason, why you’d even need a library like
Decimal. Because floats are not suited for calculating correctly.CharlesO
Every single finance department i have dealt with keeps parallel records in excel.
For some some reason there is an innate distrust of records from IT systems
NobbZ
No, I do get
9:LostKobrakai
sribe
Bah. I only looked at the text in the post. Did not notice the “this file has been truncated”…
Last Post!
OvermindDL1
That’s horrifying… They really need to be taught how very very VERY bad that is…
If they want to use a Microsoft product then they should use Access instead of Excel, it can do everything Excel can except it has types, like a Decimal type, that does things right for such numbers…