LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2015, 07:59 AM   #16
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941

S'pose?

For the record, another way to do this is the way that COBOL did it: "packed decimal," otherwise known as "BCD = Binary Coded Decimal."

In this representation, groups of 4 bits (i.e. hexadecimal digits ...) represent decimal digits. The rightmost 4-bit group represents the sign of the number, using one of the six bit-patterns that are not $0 through $9. So, for example, in a 32-bit word, you could store 7 digits plus the sign.

Even the lowliest microprocessors support a decimal mode.

There's another fundamental reason why computer floating-point numbers are not a good choice for dollars-and-cents calculation: the exponent of the number is actually base-two, not base-ten. Just as it is impossible to precisely represent "1/3" in decimal ("0.3333..."), it is for the same reason impossible to precisely represent "1/10" in a float-binary.

Although the differences are very small, one must always remember that "an accountant is someone who'll spend a dollar to find a dime."

It has been ground into their heads ... and into the heads of "failed accountants" who therefore Go To The Dark Side, becoming regulators and tax-officials ... that "the books must balance 'to the penny.'" Woe be it to anyone who doesn't pay careful attention to that.
 
Old 03-24-2015, 12:50 PM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You're putting in an awful lot of horsepower into analyzing this particular issue, you've found that single precision floating point will not suffice. Eventually if the numbers get large enough, double precision will no longer suffice. You have a ton of open issues related to tracking information related to your finances. I'd stick a fork in the whole math subject by ensuring that you have financial calculations which are rock solid via integer conversion. Consider also that I do not believe you are calculating fractional results either, correct? You are taking the results reported which are numbers from statements, even if the transaction resulted in fees or earnings which were expressed in lengthy percentages like 2.9995%, the statements you receive and the actual amounts given are already rounded and so you would either receive $0.02 on one dollar, or $0.03 on one dollar. Or in fact if it involved $1000.00 then the 2.9995% would mean that you would receive either $29.99 or $30.00, but that number would be in the statement and while you might validate that their calculations were close, you would also have the exact figures which they gave to you. If that point were in error, you could raise it there. And then if they don't help you calculate the total gain or loss, you could report it properly on your own.

I think it's two parts (1) validating that each individual report is as accurate and complete as it needs to be, and (2) determining how you report your total status for your taxes. Because after all, per your example of buying, selling, buying again and finally re-selling. You could execute the two purchases using different brokers, correct? So it would be up to you to ensure your summary of reporting were correct, and also individually ensure that each broker house reported the individual transactions correctly.
 
Old 03-24-2015, 09:05 PM   #18
EldonCool
Member
 
Registered: Dec 2010
Location: Nevada
Distribution: Slackware
Posts: 258

Original Poster
Rep: Reputation: 3
Quote:
Because after all, per your example of buying, selling, buying again and finally re-selling. You could execute the two purchases using different brokers, correct? So it would be up to you to ensure your summary of reporting were correct, and also individually ensure that each broker house reported the individual transactions correctly.
It used to be that each individual indiviually figured out his gains
and losses. The thing that changed was what the Brokers had to report
to the IRS. That changed about 3 years into the Obama administration.
Now Brokers are supposed to report these disallowed losses. The
problem seems to be that programmers can't write the code to actually
figure it out, so they are reporting garbage to the IRS, and if you
use the broker's figures, you can get hosed. I went through this
last year at tax time, I had broker reports showing tens of thousands
of disallowed losses, therefore my tax was increased thousands if I
let them get away with it. I gave detailed accounts of each situation
to my tax prepare person and they would then report on the form what
the broker said, law required it, then make another entry wiping
out the disallowed BS and included my worksheet showing all transactions.

It is pretty surprising to me the accuracy of the calculations from
the broker. I have 3 brokers right now, and one just yesterday, had
a situation where they made a correction of plus $75 to my account
for a shortage they had made before, and I had sold some options and
received $4840.71 from that.

So I just received 75 + 4870.71, and they reported my new cash balance
as 115,576.94.

My cash balance before these things was 110,661.24. So do this addition,

110,661.24
__4,840.71
_____75.00
-----------

do you get 115,576.94. My computer doing that math gets one cent
more, and this was from just making two additions.

The problem is really not the one cent error, the problem
is the additional requirements the IRS has put into regulation
on reporting of gains and losses by brokers. There is actually
a cottage industry springing up to overcome this BS the IRS is
putting onto us. Some are selling programs that keep these
figures straight and then print a correct 8949 form for you
at tax time.

Believe me, I know how to get around all the crap the IRS puts
out, but it does make life more complicated.

Also, I think it makes the tax calculations so hard, that
auditors are probably going to go nuts trying to keep up
with their own rules. Making things this complex, and then
having to go through mounds of this crap in an audit must
make auditors life hell. I got nothing against making IRS
auditors life hell, so let the fun continue.

Last edited by EldonCool; 03-24-2015 at 11:04 PM.
 
Old 03-24-2015, 10:49 PM   #19
EldonCool
Member
 
Registered: Dec 2010
Location: Nevada
Distribution: Slackware
Posts: 258

Original Poster
Rep: Reputation: 3
Quote:
For the record, another way to do this is the way that COBOL did it: "packed decimal," otherwise known as "BCD = Binary Coded Decimal."
Ah yes, COBOL, I have not heard that name in years.
My first job as a programmer was as a COBOL programmer.
In those days over 90 percent of programmer jobs was in COBOL.

I was in a community college majoring in computer science,
I recently retired from the Air Force, my wife, a nurse had
gone into the army and we were stationed at Fort Dix.
I stayed home and took care of the kids and she went to
work. For entertainment I enrolled in college and took
up computers. I had the highest average of all the guys
in the computer science area and I was about half way
through COBOL, when the college wanted to higher a
part time programmer, all their applications were in
COBOL. My instructor recommended me to the computer
department, and I had my first computer job. It was
pretty entertaining.

That was in 1979 or 80. How times change, I never
hear of COBOL now, and I have programmed in 15 languages
since that day.

Retired in about 2003, but still program some for my own
use and entertainment.

Last edited by EldonCool; 03-24-2015 at 10:57 PM.
 
Old 03-28-2015, 12:55 PM   #20
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
This thread illustrates why Cobol (COmmon Business-Oriented Language) always uses integer math in the base language, no need to add library functions or jump through coding hoops.

There were at least 3 ways to represent numbers within a Cobol program: display (PIC 9), binary, (either litlle- or big-endian) and packed decimal, all computations would convert fractions to integers automatically.

Last edited by dogpatch; 03-28-2015 at 01:00 PM.
 
Old 03-28-2015, 01:39 PM   #21
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Well, to be fair, the IEEE's floating point standard wasn't around when COBOL was designed.
 
Old 03-28-2015, 02:56 PM   #22
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Correct me if I'm wrong, but I think Fortran is about as old as Cobol, and used its own floating point numeric, not unlike IEEE. Before C, it was: Fortran for scientific programs, Cobol for business.
 
Old 03-28-2015, 09:30 PM   #23
EldonCool
Member
 
Registered: Dec 2010
Location: Nevada
Distribution: Slackware
Posts: 258

Original Poster
Rep: Reputation: 3
yeh, Fortran is probably as old as COBOL, I am guessing older. I don't
remember that much about it's number types though, never did like the
language much, but got stuck with it in one of my main jobs for about 12
years. Never did learn to like it.
 
Old 03-29-2015, 06:39 PM   #24
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Fortron = 1957
COBOL = 1959

Both quite old and outdated now.
 
Old 03-29-2015, 11:48 PM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Indeed, fortran is quite outdated: standard fortran2015 isn't defined yet, we have to use fortran2008; cobol is a bit better, it has version cobol2014
https://en.wikipedia.org/wiki/Fortran#Fortran_2008
https://en.wikipedia.org/wiki/COBOL#COBOL_2014

Last edited by NevemTeve; 03-29-2015 at 11:52 PM.
 
Old 03-30-2015, 12:27 AM   #26
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by NevemTeve View Post
Indeed, fortran is quite outdated: standard fortran2015 isn't defined yet, we have to use fortran2008; cobol is a bit better, it has version cobol2014
https://en.wikipedia.org/wiki/Fortran#Fortran_2008
https://en.wikipedia.org/wiki/COBOL#COBOL_2014
Just because there are modern standards out doesn't mean the language isn't outdated for the most part. How much software do you see written in Fortran or COBOL in the wild?
 
Old 03-30-2015, 03:13 AM   #27
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
For example, right here in this Forum fortran-related question do come up regularly.
 
Old 03-30-2015, 05:55 AM   #28
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by smeezekitty View Post
Just because there are modern standards out doesn't mean the language isn't outdated for the most part. How much software do you see written in Fortran or COBOL in the wild?
Every day. Fortran is very much alive and well in the scientific community.
 
Old 03-30-2015, 07:25 AM   #29
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Using a spreadsheet is much easier than a C program for the original problem.
In a C program doing personal accounting, double is the best data type. float is simply not accurate enough. double is accurate enough and simple and efficient to use. Anything more accurate than double requires significant extra effort by the programmer.

If you were coding the securities portfolio analysis for financial institutions (something I actually did in the distant past) you would certainly not use double. You would select a data type that will provably be accurate to the penny on absurdly large computations every time.

Even in home accounting, it is harder than you might expect to prove that every computation in your home accounting algorithms will be correct to the penny. double has enough precision for the amounts to be correct to the penny. But provably correct is harder. As an active investor, who keeps duplicate records and compares to brokerage records (rather than trusting brokerage records) I know that "usually correct to the penny" is an important standard for home accounting for those few of us who make such comparisons. But "always correct to the penny" is a completely pointless goal. The brokerage is NOT always correct to the penny. Part of comparing is adjusting my records to match their records when they are off by a penny. That is one of the things easier in a spreadsheet than in a C program:

Example: company X spins off company Y at 1 for 8. I had 50 shares of company X, so I get 6.25 shares of company Y. My broker automatically merges my .25 shares of Y with fractional shares of all their other customers in similar situations and sells those whole shares and proportionally distributes that cash. Now my cost basis for X has been split into 3 parts: Part is remaining reduced cost basis for X, which will matter someday when I sell those shares. Part is cost basis for my 6 shares of Y. Part is cost basis for the sale of .25 shares of Y. There is a legally specified formula for computing those three numbers and I plug that into my spreadsheet. Then my brokerage account shows their 3 numbers, one is a penny high and two are each a penny low. So one cent of cost basis has vanished entirely, and the gain on the fractional share is one cent higher than it really was. That is just an annoyance of investing. Comparison is to find the brokerage's big errors. They make little errors and you just fix your records to duplicate their little errors.

Quote:
Originally Posted by suicidaleggroll View Post
Every day. Fortran is very much alive and well in the scientific community.
In my experience, having had to maintain that crap on several occasions, I can confirm "alive" and contest "well". Fortran is mostly used by stubborn non programmers who write terrible code, both because they aren't programmers and because it is a terrible language. But there is too much cultural inertia to throw it out and use easier languages for the small projects and C++ for the big projects. (C++ is very flawed. But every other language in common use for big projects, very much including Fortran, is so unsuited to big projects that its continued use is insane).

Last edited by johnsfine; 03-30-2015 at 07:36 AM.
 
Old 03-31-2015, 12:30 PM   #30
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
http://t.co/dZwke4cmnS



The other thing to be aware of when using floating point numbers is that comparing them for equality is more complex.

Last edited by dugan; 03-31-2015 at 04:28 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add users to Multiple Computers at Once? DiskChris Linux - General 3 12-16-2012 09:39 AM
How to subtract days HyperTrey Programming 15 06-06-2008 03:45 AM
How to subtract two date in Perl? abdul_zu Programming 8 12-08-2007 09:59 AM
Security risk for Windows workgroup computers if I add a Linux webserver? rreiss Linux - Security 1 03-17-2006 12:51 PM
using bc to subtract hexdemical schurt Linux - Software 1 01-07-2005 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration