BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Avoiding floating points
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical
View previous topic :: View next topic  
Author Message
Jonathan Neve[Microtec]
Guest





PostPosted: Thu Apr 26, 2007 8:12 am    Post subject: Avoiding floating points Reply with quote



Hi all,

I hate floating points with a passion. Does anyone know of a simple way
to get rid of them and somehow be able to do elemetary calculations
without random imprecisions and unpredicitble and meaningless error
messages popping up out of no-where?

Thanks!
--
Best regards,
Jonathan Neve
_______________
CopyCat - advanced Delphi components for database replication,
supporting CodeGear Interbase, FirebirdSQL and MS SQL Server!
_______________
CopyTiger - the ultimate database replication solution for CodeGear
Interbase, FirebirdSQL and MS SQL Server!
_______________
More information : http://www.microtec.fr/copycat
Back to top
Jonathan Neve[Microtec]
Guest





PostPosted: Fri Apr 27, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote



Brad White wrote:

Quote:
David Farrell-Garcia wrote:
Jonathan Neve[Microtec] wrote:

If your application processes everything to integers or
rounds to a fixed number of decimal places why do you care how the
database stores it? 5 = 5.000000000 no matter how you slice it.
But that only applies to a few numbers.
For example, on the face of it, you would expect it
to apply to 0.1, but it does not.

Instead of 0.1 = 0.100000000
you get 0.1 = 0.100,000,000,000,000,000,001,355,252,715,606,880,
542,509,316,001,087,427,139,282,226,562,5

No matter how you store 0.1 in the database, you will
get something else back out.
Or did I miss your point?

Brad.

Exactly, and for most business applications, that's simply unacceptable.

Anyway, the point of this thread was, I admit, primarily a rant -- I'm
sick of floating point imprecisions, on the one hand, and their weird
errors (perhaps compiler-related) on the other. In none (or hardly any)
of my applications can I conceive a need for decimal points that float
around, I *always* want to get back the *exact* decimal representation
that I put into my database.

So I'm not really trying to change the face of computer programming,
I'm just frustrated with floats, and I need to make an effort to
replace them with something more suitable for my needs. I suppose BCDs
or simply Currency fields would be good, and I'll make sure I never
make another application with floats... :)

--
Best regards,
Jonathan Neve
_______________
CopyCat - advanced Delphi components for database replication,
supporting CodeGear Interbase, FirebirdSQL and MS SQL Server!
_______________
CopyTiger - the ultimate database replication solution for CodeGear
Interbase, FirebirdSQL and MS SQL Server!
_______________
More information : http://www.microtec.fr/copycat
Back to top
Paul Scott
Guest





PostPosted: Fri Apr 27, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote



On Thu, 26 Apr 2007 20:13:30 +0100, John Herbster
<herb-sci1_AT_sbcglobal.net> wrote:
Quote:
Having those worthless date-times scattered about
the EXEs makes detecting other changes more of a
problem that it need be.

Ah, so *that's* the reason the exe's are different! (or one of the
reasons?)

I was supplied with two versions of an exe built only seconds apart - the
only difference was a one character change to the MainForm's caption - one
was "v5.40" the other "v5.41". (The changed caption was an easy visual
check that when testing that our "new" DLL generated exactly the same
results as the old version, that we were actually running the appropriate
app+DLL combination)

I expected a "fc /b" to show only one byte difference - but I was wrong -
and by several orders of magnitude!

--
Paul Scott
Information Management Systems
Macclesfield, UK.
Back to top
AlexB
Guest





PostPosted: Sat Apr 28, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote

Mark Andrews (The Other One) wrote:

Quote:
I simply do not buy the idea that assigning zero is failing. We've
all had situations where we start to doubt the compiler, the CPU and
the arrangement of the planets. In every one of those cases I've
eventually (often after a lot of sweat and tears) located the problem
in my own code--sometimes a subtle memory overwrite problem.

Mostly agreed.
But (as I've described in this thread earlier) I really got problem
with code generation in small test program (where I had no chance to
make my own mistake).

--
Alex
Back to top
AlexB
Guest





PostPosted: Sat Apr 28, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote

John Herbster wrote:

Quote:
... Wrong code generation.

Does anyone know of a QualityCentral report on this?

I think no.
QC requires reproducing steps. This issue looks like ghost and (I
think) can't be reproduced reliably.

Some days ago I'd met with this issue again and again with the same
ActiveX (new version). Steps was:
- debug session, internal structured exception in ActiveX (0xc0000008-
invalid handle)
- some correction in program and rebuild
- new debug session and wrong results in FP constant assignment (x=1.0;
actual x value is trash)
- CPU window shows wrong address for constant (1.0) in this line (but
correct address for this constant in some other lines)
- rebuild had no effect
- new small test project in the same IDE session - the same result
(wrong address)
- reload IDE
- test progect now compiles and works OK
- rebuild of the first project - all seems OK.

Quote:
Has it be fixed in later versions or updates?
I'd found this issue in BCB5.

Jonathan, what version you use?

Quote:
Is or was there a workaround?
Reload IDE, rebuild all or rebuild all with command line compiler.

Then do 'Make' only and avoid recompiling of 'problematic' unit (i.e.
use 'good old' obj for link) where compiler generates wrong addresses
for FP constants ...

--
Alex.
Back to top
Mark Andrews (The Other O
Guest





PostPosted: Sat Apr 28, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote

John Herbster wrote:
Quote:
I have made
two QC reports that are related to the subject:
http://qc.borland.com/wc/qcmain.aspx?d=5928
http://qc.borland.com/wc/qcmain.aspx?d=8399

Great detective work John. I hadn't been aware of this problem before.

Quote:
In the first, Danny Thorpe's replies were interesting

If by "interesting" you mean borderline dismissive, then yes.

Quote:
but he did not want to fix all parts of the problem.

I'll be implementing your workaround. Thanks.

Any idea why #5928 is marked closed?

Cheers,
Mark.
Back to top
AlexB
Guest





PostPosted: Sat Apr 28, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote

Mark Andrews (The Other One) wrote:

Quote:
I simply do not buy the idea that assigning zero is failing. We've
all had situations where we start to doubt the compiler, the CPU and
the arrangement of the planets. In every one of those cases I've
eventually (often after a lot of sweat and tears) located the problem
in my own code--sometimes a subtle memory overwrite problem.

AFAIK text (code) and constant data segments are read-only. How memory
overwriting can be the reason of wrong FP constant address appearance?
--
Alex.
Back to top
Mark Andrews (The Other O
Guest





PostPosted: Sat Apr 28, 2007 8:12 am    Post subject: Re: Avoiding floating points Reply with quote

M.E. wrote:
Quote:
"John Herbster" <herb-sci1_AT_sbcglobal.net> wrote in message
news:46309033 (AT) newsgroups (DOT) borland.com...
Please, please, anyone:
Give me code for an example of where assigning 0 fails!

I can't. It does not fail "regularly". It does fail randomly, and it *does*
fail. Like the OP stated.

I call shenanigans. (I also can't believe some of the things I'm reading
in this thread.)

I simply do not buy the idea that assigning zero is failing. We've all
had situations where we start to doubt the compiler, the CPU and the
arrangement of the planets. In every one of those cases I've eventually
(often after a lot of sweat and tears) located the problem in my own
code--sometimes a subtle memory overwrite problem.

I'm not saying that codegen bugs don't exist, but this particular claim
needs some rather compelling evidence.

Cheers,
Mark.
Back to top
Loren Pechtel
Guest





PostPosted: Sun Apr 29, 2007 7:56 am    Post subject: Re: Avoiding floating points Reply with quote

On 26 Apr 2007 14:45:56 -0700, "Nicholas Ring"
<Nicholas.Ring (AT) smf (DOT) com.au> wrote:

Quote:
Just wondering if there was a

Loren Pechtel wrote:

On 26 Apr 2007 05:54:42 -0700, "AlexB" <b.a.v (AT) inbox (DOT) ru> wrote:
Next semester the teacher asks me about it. I utterly astounded the
whole department with the following:

10 X = 3000000
20 X = X + 1
30 If X < X + 1 then goto 10
40 Print "X = X + 1"


Just wondering if line 30 has a typo, in that it should be going to
line 20, not line 10?

Of course--you don't really think I cut & pasted the old code, do you?
Back to top
Loren Pechtel
Guest





PostPosted: Sun Apr 29, 2007 7:56 am    Post subject: Re: Avoiding floating points Reply with quote

On Thu, 26 Apr 2007 17:19:19 -0500, "John Herbster"
<herb-sci1_AT_sbcglobal.net> wrote:

Quote:

10 X = 3000000
20 X = X + 1
30 If X < X + 1 then goto 10
40 Print "X = X + 1"

"Nicholas Ring" <Nicholas.Ring (AT) smf (DOT) com.au> wrote
Just wondering if line 30 has a typo, in that
it should be going to line 20, not line 10?

Nicholas, I think that you are right. I looked at it,
noted the concept that Loren was intending, and
overlooked that error.

Will this ever finish?
var x: int64;
begin
x := 0;
While (x < high(x)) do inc(x);
ShowMessage('Finished with x='+IntToStr(x));
end;

Rgds, JohnH

Yup. It finishes when x = high(x).
Back to top
Loren Pechtel
Guest





PostPosted: Sun Apr 29, 2007 7:56 am    Post subject: Re: Avoiding floating points Reply with quote

On 26 Apr 2007 09:32:35 -0700, "Jonathan Neve[Microtec]"
<jonathan (AT) microtec (DOT) fr> wrote:

Quote:
I don't want integers, I want decimal-point numbers that "work" (from a
user perspective), and so I'm not interested in the decimal point
"floating"... Wink Yes, yes, I know, I can hack it out by hand, I was
primarily ranting at the utter uselessness of floats (for most of the
practical purposes they're usually used for -- I don't doubt that they
are useful for some people), and secondarily wondering what other
people do to handle common every-day business operations (dealing with
money, or any other sensitive data that has to be given back exactly
the way it was put in).

Floats are fine when you don't need perfect precision (note that in
many cases perfect precision is unattainable anyway.) If you need it
to be perfect (example: most financial calculations) then do it fixed
point. My approach to handling this sort of problem is define a unit
that all internal variables will be stored in that has no decimal
points--it's only scaled on input or output.

I have never used the Currency type because it's *NOT* an
answer--having too many decimal points will cause trouble in some
cases. When you're doing financial calculations you generally want
the fractional pennies to vanish, not get carried.
Back to top
Loren Pechtel
Guest





PostPosted: Sun Apr 29, 2007 7:56 am    Post subject: Re: Avoiding floating points Reply with quote

On Thu, 26 Apr 2007 12:08:14 -0500, "John Herbster"
<herb-sci1_AT_sbcglobal.net> wrote:

Quote:

"Jonathan Neve[Microtec]" <jonathan (AT) microtec (DOT) fr> wrote
... the compiler produces different code from the same
source depending on its mood does seem to me to be
a compiler bug. ...

It is not the compiler's mood; it is the date-time of
compilation. I don't know about the later compilers
in this regard, but D7 and earlier compiler encoded
the date in two or three dozen places within the EXE.

And yes, I think that it is a bug, too.

I have suspected (but never proved) that there are cases where the
compiler emits pure garbage. I have grown suspicious of garbage bytes
due to alignment. If so, this is a totally harmless failure other
than making the .EXE's not match so I've never dug into it.
Back to top
Loren Pechtel
Guest





PostPosted: Sun Apr 29, 2007 7:56 am    Post subject: Re: Avoiding floating points Reply with quote

On Fri, 27 Apr 2007 10:31:42 +0200, "Anders Isaksson" <i.rather@not>
wrote:

Quote:
John Herbster wrote:

It is not the compiler's mood; it is the date-time of
compilation. I don't know about the later compilers
in this regard, but D7 and earlier compiler encoded
the date in two or three dozen places within the EXE.

It's also a question of the linker. Any disk sector of the EXE that
isn't filled up completely by compiled/linked code (or data) will
contain whatever bytes was there before being assigned to the current
file. Code alignment will create such 'holes' in the disk image, all
over the place, containing 'random' data from your previously erased
files.

At least up to D5, I haven't taken a closer look at later versions.

But the whole sector is probably going to be written anyway, you won't
pick up garbage bytes off the disk.

I do think you pick up garbage bytes from RAM this way, though.
Back to top
John Herbster
Guest





PostPosted: Sun Apr 29, 2007 8:11 am    Post subject: Re: Avoiding floating points Reply with quote

"Loren Pechtel" <lorenpechtel (AT) hotmail (DOT) invalid.com> wrote
Quote:
... you won't pick up garbage bytes off the disk.
I do think you pick up garbage bytes from RAM
this way, though.

Loren, et. al,
I would like to see some proof or examples of this.
Rgds, JohnH
Back to top
David Erbas-White
Guest





PostPosted: Mon Apr 30, 2007 8:11 am    Post subject: Re: Avoiding floating points Reply with quote

Loren Pechtel wrote:
Quote:
On 28 Apr 2007 00:58:46 -0700, "AlexB" <b.a.v (AT) inbox (DOT) ru> wrote:

Mark Andrews (The Other One) wrote:

I simply do not buy the idea that assigning zero is failing. We've
all had situations where we start to doubt the compiler, the CPU and
the arrangement of the planets. In every one of those cases I've
eventually (often after a lot of sweat and tears) located the problem
in my own code--sometimes a subtle memory overwrite problem.
AFAIK text (code) and constant data segments are read-only. How memory
overwriting can be the reason of wrong FP constant address appearance?

Segments? Segments are effectively not used anymore.

Were they ever 'effectively' used??? <G>

David Erbas-White
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.