Board index » delphi » Avoiding floating points

Avoiding floating points


2007-04-26 04:03:57 PM
delphi172
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 : www.microtec.fr/copycat
 
 

Re:Avoiding floating points

Quote
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?
Use the currency type?
Oliver Townshend
 

Re:Avoiding floating points

Quote
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?
As in "using whole numbers"? I'd use a very simplistic method. Multiply
all floats by a specific amount (100, 1000, 10000 or whatever) and then
either round or trunc them (depends on how brutal you want to be.) You then
just need to divide by the same amount to get the real number. That can be
done with div and mod - you therefore never need to deal with real numbers.
I wouldn't advocate using this technique for anything that really mattered
though. It could cause any real application using floats to fail. Would be
good work around for fast simple floating point calcs, though.
 

Re:Avoiding floating points

Jonathan Neve[Microtec] writes:
Quote
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?
I think, you should use Turing machine. It is very reliable and
theoretically can solve any algorithmic problem.
--
Alex.
 

Re:Avoiding floating points

Oliver Townshend writes:
Quote
>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?

Use the currency type?
Interesting idea. I have never used the Currency type, and hardly knew it
existed. The trouble of course, is that most of my databases are
Interbase dialect 1, and so all my fields are of course converted to
floats...
Does anyone have a good case for the continued existence and widespread
use of floats? In other words, is there anything that they're good for,
other than wasting hours and causing headaches?
I just had a bug that I spent a few hours on. I was assigning 0 to a
double. On the very next line, for no apparent reason, the de{*word*81}
showed that the variable had actually received the value 1.5E104, which
in turn obviously caused a floating-point overflow not long after. This
was code that had been working for months, and that I hadn't touched
recently... Restarting BCB, even rebooting, recompiling any number of
times, made no difference. Until I do a complete rebuild, and then all
works well...
How more unreliable can you get? Compile it once, and it works, compile
it again, and it stops working, compile it yet again, without making
any source code changes, and suddenly, it starts working again --
though for how long, none can tell! The FPU seems to be the most
badly-designed (or badly-tested) component of our PCs, and has been a
source of much frustration for many years, for no practical advantage
-- why do we keep on using it?!? When will someone make a compiler
where "double" and "float" can merrily resolve to bcd? :)
--
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 : www.microtec.fr/copycat
 

Re:Avoiding floating points

AlexB writes:
Quote
Jonathan Neve[Microtec] writes:

>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?

I think, you should use Turing machine. It is very reliable and
theoretically can solve any algorithmic problem.
What exactly do you mean? I am just trying to do elementary stuff, like
multiply two numbers, divide others, and add them up -- nothing so
complex that it should be unpredictible and erratic....
--
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 : www.microtec.fr/copycat
 

Re:Avoiding floating points

"Jonathan Neve[Microtec]" <XXXX@XXXXX.COM>writes
Quote

Does anyone have a good case for the continued existence and widespread
use of floats?
Was that a joke?
 

Re:Avoiding floating points

M.E. writes:
Quote
>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?

As in "using whole numbers"? I'd use a very simplistic method.
Multiply all floats by a specific amount (100, 1000, 10000 or
whatever) and then either round or trunc them (depends on how brutal
you want to be.) You then just need to divide by the same amount to
get the real number. That can be done with div and mod - you
therefore never need to deal with real numbers.

I wouldn't advocate using this technique for anything that really
mattered though. It could cause any real application using floats to
fail. Would be good work around for fast simple floating point calcs,
though.
Yeah, sure, I do this kind of thing sometimes, but it is not practical,
and since floats often do work, that is what I usually end up using,
especially considering the legacy of code that uses floats. it is just
maddening when it stops working for absolutely no reason -- I must have
just looked at it wrong while it was compiling, and so it got offended
or something...
--
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 : www.microtec.fr/copycat
 

Re:Avoiding floating points

"Jonathan Neve[Microtec]" <XXXX@XXXXX.COM>writes
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?
When using Delphi.net I'd suggest System.Decimal.
It does not have any rounding errors.
Regards,
Jeroen Vandezande
 

Re:Avoiding floating points

Uffe Kousgaard writes:
Quote
"Jonathan Neve[Microtec]" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...
>
>Does anyone have a good case for the continued existence and
>widespread use of floats?

Was that a joke?
Not really, but you needn't take it too seriously. I am just very
frustrated, and would personally live a much more peaceful life if I
could avoid ever using floats... But I assume they must have some
limited value to a small segment of humanity, or we wouldn't still be
plagued by them...
--
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 : www.microtec.fr/copycat
 

Re:Avoiding floating points

Jonathan Neve[Microtec] writes:
Quote
>Use the currency type?
Interesting idea.
Seems coprocessor and data types remain above suspicion.
Quote
I just had a bug that I spent a few hours on. I was assigning 0 to a
double. On the very next line, for no apparent reason, the de{*word*81}
showed that the variable had actually received the value 1.5E104,
which in turn obviously caused a floating-point overflow not long
after.
Wrong code generation. I had it 2 times in BCB5 (2-3 years ago and some
days ago again). Compile, set breakpoint on your
v:=0;
On breakpoint see asm code in CPU window (Ctrl+Alt+C).
Note the address of constant (0.0). In my case in first time it was
address ... of next subroutine! In second time it was just wrong
address.
I don't know why it happens. In my case it coincided with use of an
imported external ActiveX component: the IDE compiler started to work
incorrectly after debug session with this component. Treatment: use
command line compiler or reboot+rebuild all.
Quote
This was code that had been working for months, and that I
hadn't touched recently... Restarting BCB, even rebooting,
recompiling any number of times, made no difference. Until I do a
complete rebuild, and then all works well...
Joking apart: Make complete rebuild each time when you see strange
behaviour of your program. :)
Quote
How more unreliable can you get? Compile it once, and it works,
compile it again, and it stops working, compile it yet again, without
making any source code changes, and suddenly, it starts working again
-- though for how long, none can tell!
If code is really wrong (as described above) - ask CodeGear.
--
Alex.
 

Re:Avoiding floating points

Jonathan Neve[Microtec] writes:
Quote
>I think, you should use Turing machine. It is very reliable and
>theoretically can solve any algorithmic problem.

What exactly do you mean? I am just trying to do elementary stuff, like
multiply two numbers, divide others, and add them up -- nothing so
complex that it should be unpredictible and erratic....
Turing machine processes only 0s and 1s with very simple rules so here
it's impossible to be mistaken. :0)
IMHO it is difficult to imagine a normal computer without a floating
point operations.
Abacus? :-)
--
Alex.
 

Re:Avoiding floating points

"Jonathan Neve[Microtec]" <XXXX@XXXXX.COM>wrote
Quote
I hate floating points with a passion. Does anyone
know of a simple way to get rid of them ...
Jonathan,
Cast a vote for QualityCentral report #28022 titled
"Add Fixed and Floating Decimal Point Numbers to Delphi"
qc.borland.com/qc/wc/qcmain.aspx
Quote
and somehow be able to do elementary calculations
without random imprecisions and unpredictable
and meaningless error messages popping up ...
An answer to that is more difficult. Suppose that
you want to divide 4 by 3, add 0.01, and then store
the result for use later. In elementary school
arithmetic we could use fractions or mixed whole and
fraction numbers. However without round-off, it gets
a little complicated.
And if you do allow for rounding the result into a
decimal fraction result (or almost any other type of
result), then you need to specify or know what
rounding rule will be applied and particularly where
in the calculation it should be applied.
I think that an answer to a large class of such
problems is to be able to use fixed and floating
*decimal* point numbers (hence my QC suggestion).
However even this requires that *every* *divide*
operation be allowed to have a rounding specifier
to determine whether 4/3 should be stored as
1.33 or 1.34. To simplify such specifications, one of
my suggestions allows for the rounding rule to be
specified with in declaration of the decimal variables.
Until such number and operation types are available
to us, and while we struggle to get by with the
common floating *binary* point numbers, you can use
my decimal rounding package available from CodeCentral.
cc.codegear.com/Item.aspx
When used properly, it can account for the small errors in
our common single, double, and extended types and can
make you think that you are working with fixed decimal
fraction numbers.
With sympathy, JohnH
--
Support the movement to add floating and fixed
decimal fraction numbers to Delphi.
qc.borland.com/wc/qcmain.aspx
Quality Central, bug status & reporting qc.borland.com/
Code Central codecentral.borland.com/codecentral/ccweb.exe
 

Re:Avoiding floating points

Quote
Yeah, sure, I do this kind of thing sometimes, but it is not practical,
and since floats often do work, that is what I usually end up using,
especially considering the legacy of code that uses floats. it is just
maddening when it stops working for absolutely no reason -- I must have
just looked at it wrong while it was compiling, and so it got offended
or something...
We had a bunch of products using Interbase. I used this technique because
Interbase and floating point numbers can be a little unpredictable,
especially when using UDF's. It works well, solong as your calculations are
to a specific amount of decimal places. Things like fixed percentages (e.g.
Each transaction requires a 4.5% cut to be paid to a specific account.) I
wouldn't have used this technique if Interbase hadn't messed with me a lot.
As for assignments... I have found that Single is extermemly bad at accepting
specific values. Double is a lot better, but I still tend to assign "0.0000"
rather than just "0" and often assigning just 0 gives the weird results you
mention. it is definately related to 0 though. Assigning whole numbers seems
to work in a reliable way.
Though, dotNet isn't much better. Passing "TDateTimes" to and from dotNet
via PInvoke gives really, really odd results. I had a DLL for a third party
product that interfaces with hardware. Its interface was written in Delphi
and used Shortstrings and TDateTimes in the API (shudder.) This causes some
issues when you need to call those API routines from C# ;-) The Shortstrings
weren't too bad (you need to shift the C# string along one char and add the
length cast to a char - pretty much - and truncate to 255 chars.) However,
TDateTime is a PITA. You can quite easily get a TDateTime alike using the
COM compatible DateTime conversion routine, but when you pass that to
Delphi, there are some odd rounding errors. it is out by something like
0.0000000052 or something equally small. This turns out to be Miliseconds,
so if you remove the partial seconds, it is all fine. All I know is, if you
view the value in VS2005, it looks to the same precision ans in Delphi, but
in reality it is not. I guess I could have written a wrapper and marshalled
the dotNet type (using DateTime.Ticks or something like that), but adding an
extra layer was not the idea! It now works well enough to be passable.The
device only time stamps in 15 second blocks anyway, so it is all fine in the
end. Just very, very perplexing. (This is an unknown version of Delphi's DLL
talking to dotNet 2.0, by the way.)
 

Re:Avoiding floating points

"Jonathan Neve[Microtec]" <XXXX@XXXXX.COM>wrote
Quote
>Use the currency type?
Interesting idea. I have never used the Currency type,
The trouble of course, is that most of my databases are
Interbase dialect 1, and so all my fields are of course
converted to floats.
Does this mean that the data is stored in the data tables
as doubles?
Quote
Does anyone have a good case for the continued
existence and widespread use of floats?
Oh yes! They are excellent for engineering and scientific
calcs, especially for modeling physical objects.
Quote
... I was assigning 0 to a double. On the very next line,
for no apparent reason, the de{*word*81} showed that
the variable had actually received the value 1.5E104,
I suspect that the variable may have been optimized out
of the sight of the de{*word*81}.
Quote
... Compile it once, and it works, compile it again, and
it stops working, compile it yet again, without making
any source code changes, and suddenly, it starts working
Each compile produces a different EXE. That is why I
submitted QC report #29538 titled
"IDE produces different EXEs from same source."
Quote
The FPU seems to be the most badly-designed (or
badly-tested) component of our PCs, and has been a
source of much frustration for many years, ...
The PC's FPU is designed to implement IEEE-754,
which I believe that it does quite well. With the
exception of the divide problem of about 10 years
ago and a VIA chipset problem with interrupts
about 8 years ago, I have found *nothing* random
about the FPU's operation.
Quote
-- why do we keep on using it?!? When will
someone make a compiler where "double" and
"float" can merrily resolve to BCD? :)
I am already guessing what you complaints about
BCD would be. <g>
With sympathy, JohnH