Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Questions about Delphi FP math spec from a C#/Java user, also some etc..


This question is answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Nov 14, 2016 11:09 AM Last Post By: Remy Lebeau (Te...
Ian McDonald

Posts: 3
Registered: 12/1/09
Questions about Delphi FP math spec from a C#/Java user, also some etc..  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 4:32 PM
Hello,

I am porting an outer totalistic cellular automaton which is somewhat similar to "rug" rules from Delphi to Java initially, and I am getting a completely different outcome than what is expected.

I am not asking anyone to check out the code, and in fact do not want this, I am simply seeking resources on the FP specification which I have as yet not had much luck finding in the very limited time I have available to devote to this.

The code is actually pretty simple as such things go, using only arrays, matrices and unary math operators, so it must be something intrinsically different under the hood which is foiling me; I was wondering if perhaps there is a difference between the way Delphi and Java or C# handle floating point arithmetic, specifically the division and modulus operators I suspect to be the issue.

Are there any other substantial and fundamental differences in the languages at this low of a level?

I cannot currently execute the Delphi code [but I have access to its output], and I do not yet have enough experience with the IDE [I come from VS2015 enterprise mostly] and Delphi itself to code up a UI to put this algorithm into in order to set watches and breakpoints, so I thought I would ask about its handling of FP math first since that has been an issue in the past when porting across languages. I'm going to look in the Delphi Berlin samples folder for a GUI app I can gut and hack this code into, then at least I can compare state after each operation, but this will take time I will not have until December. >__<

Thanks,
Ian
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Questions about Delphi FP math spec from a C#/Java user, also some etc..
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 12, 2016 2:28 AM   in response to: Ian McDonald in response to: Ian McDonald
Ian McDonald wrote:

Hello,

I am porting an outer totalistic cellular automaton which is somewhat
similar to "rug" rules from Delphi to Java initially, and I am
getting a completely different outcome than what is expected.

I am not asking anyone to check out the code, and in fact do not want
this, I am simply seeking resources on the FP specification which I
have as yet not had much luck finding in the very limited time I have
available to devote to this.

Delphi has basically three floating-point types, single, double, and
extended. They differ in the number of bytes they occupy (4, 8, 10) and
the value range they can represent. See
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Delphi_Data_Types

An excellent article on floating point numbers and Delphi can be found
here:

http://rvelthuis.de/articles/articles-floats.html

It also goes into the internal layouts and value ranges. The format of
the single and double types is defined by an industry standard (IEEE
754) and directly supported by FPUs. The extended type is basically an
internal representation used by Intel FPUs.

Since compilers for Windows generally use the same single and double
types (although they may be named differently) the fact that you get
different results in your port either means that you have used the
wrong floating point type in the target language, or that the compiler
in question optimizes floating point expressions differently (sequence
matters here due to the limited precision) or uses a different FPU
control word setting (e.g. a different rounding mode).


The code is actually pretty simple as such things go, using only
arrays, matrices and unary math operators, so it must be something
intrinsically different under the hood which is foiling me; I was
wondering if perhaps there is a difference between the way Delphi and
Java or C# handle floating point arithmetic, specifically the
division and modulus operators I suspect to be the issue.

Are there any other substantial and fundamental differences in the
languages at this low of a level?

I cannot currently execute the Delphi code [but I have access to its
output], and I do not yet have enough experience with the IDE [I come
from VS2015 enterprise mostly] and Delphi itself to code up a UI to
put this algorithm into in order to set watches and breakpoints, so I
thought I would ask about its handling of FP math first since that
has been an issue in the past when porting across languages. I'm
going to look in the Delphi Berlin samples folder for a GUI app I can
gut and hack this code into, th en at least I can compare state after
each operation, but this will take time I will not have until
December. >__
Thanks,
Ian


--
Peter Below
TeamB

Ian McDonald

Posts: 3
Registered: 12/1/09
Re: Questions about Delphi FP math spec from a C#/Java user, also some etc..  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 12, 2016 4:19 PM   in response to: Peter Below in response to: Peter Below
Excellent information, thank you very much!

There is definitely something going on between Delphi and Java, as the person I am collaborating with took a split 1d box blur from Java that had manifested horizontal and vertical artifacts, ported it 1 to 1 over to Delphi, and the banding went away. I do not know the precise internal issue, but it is good to know that it is indeed FP math related. It is truly one beast of a problem to resolve; the behavior is entirely different! lol!

If I could embed images here I could show what I mean. In all of my 23 years in the industry, low level issues I find to be the most annoying. Tracking down memory address stuff in C might be there with it, or debugging HAL stuff and bricking...but that is even lower level haha.

I will review the links you provided and if I find what I am looking for I'll mark your reply as the answer.

Thanks!
Ian

Peter Below wrote:
Ian McDonald wrote:

Hello,

I am porting an outer totalistic cellular automaton which is somewhat
similar to "rug" rules from Delphi to Java initially, and I am
getting a completely different outcome than what is expected.

I am not asking anyone to check out the code, and in fact do not want
this, I am simply seeking resources on the FP specification which I
have as yet not had much luck finding in the very limited time I have
available to devote to this.

Delphi has basically three floating-point types, single, double, and
extended. They differ in the number of bytes they occupy (4, 8, 10) and
the value range they can represent. See
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Delphi_Data_Types

An excellent article on floating point numbers and Delphi can be found
here:

http://rvelthuis.de/articles/articles-floats.html

It also goes into the internal layouts and value ranges. The format of
the single and double types is defined by an industry standard (IEEE
754) and directly supported by FPUs. The extended type is basically an
internal representation used by Intel FPUs.

Since compilers for Windows generally use the same single and double
types (although they may be named differently) the fact that you get
different results in your port either means that you have used the
wrong floating point type in the target language, or that the compiler
in question optimizes floating point expressions differently (sequence
matters here due to the limited precision) or uses a different FPU
control word setting (e.g. a different rounding mode).


The code is actually pretty simple as such things go, using only
arrays, matrices and unary math operators, so it must be something
intrinsically different under the hood which is foiling me; I was
wondering if perhaps there is a difference between the way Delphi and
Java or C# handle floating point arithmetic, specifically the
division and modulus operators I suspect to be the issue.

Are there any other substantial and fundamental differences in the
languages at this low of a level?

I cannot currently execute the Delphi code [but I have access to its
output], and I do not yet have enough experience with the IDE [I come
from VS2015 enterprise mostly] and Delphi itself to code up a UI to
put this algorithm into in order to set watches and breakpoints, so I
thought I would ask about its handling of FP math first since that
has been an issue in the past when porting across languages. I'm
going to look in the Delphi Berlin samples folder for a GUI app I can
gut and hack this code into, th en at least I can compare state after
each operation, but this will take time I will not have until
December. >__ >
Thanks,
Ian


--
Peter Below
TeamB

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Questions about Delphi FP math spec from a C#/Java user, also someetc..  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 14, 2016 11:09 AM   in response to: Peter Below in response to: Peter Below
Peter wrote:

Delphi has basically three floating-point types, single, double,
and extended.

It also has Real48 and Currency floating-point types. Note that it also
has a Real type, but it is just an alias for Double.

Also, Extended is also just an alias for Double on 64bit systems, so there
is a loss of precision. If you need higher-precision arithmetic on 64bit,
you can use TExtended80Rec.

http://docwiki.embarcadero.com/RADStudio/en/Delphi_Considerations_for_Multi-Device_Applications#The_Extended_Data_Type_Is_2_Bytes_Smaller_on_64-bit_Windows_Systems

They differ in the number of bytes they occupy (4, 8, 10) and
the value range they can represent. See
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Delphi_Data_Types

Also see:

http://docwiki.embarcadero.com/RADStudio/en/Simple_Types_(Delphi)#Real_Types

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Questions about Delphi FP math spec from a C#/Java user, also some etc..
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 13, 2016 4:23 PM   in response to: Ian McDonald in response to: Ian McDonald
Ian McDonald wrote:

Hello,

I am porting an outer totalistic cellular automaton which is somewhat
similar to "rug" rules from Delphi to Java initially, and I am
getting a completely different outcome than what is expected.

FWIW, Delphi uses IEEE-754 compatible types. Your C# implementation may
be using double precision (binary64 in the new specs), while Delphi
generally uses extended precision (80 bit precision).

Now if you use 64 bit, both implementatins will very likely use SSE,
and that is limited to 64 bit doubles too.

--
Rudy Velthuis http://www.rvelthuis.de

"... one of the main causes of the fall of the Roman Empire was
that, lacking zero, they had no way to indicate successful
termination of their C programs." -- Robert Firth
Ian McDonald

Posts: 3
Registered: 12/1/09
Re: Questions about Delphi FP math spec from a C#/Java user, also some etc..  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 13, 2016 7:21 PM   in response to: Ian McDonald in response to: Ian McDonald
The resources provided by the community were what I was looking for.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02