unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Double-precision floating point arithmetics behaves differently in Guile and in C
@ 2021-09-26  8:45 Panicz Maciej Godek
  2021-09-26 11:55 ` Panicz Maciej Godek
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Panicz Maciej Godek @ 2021-09-26  8:45 UTC (permalink / raw)
  To: Guile User

Hi,

I recently ran into a very weird problem. I am using code for computing
Singular Value Decomposition that's been ported do Scheme by Gerald Sussman
as a part of the scmutils package (more specifically, I use some bits of
code extracted from guile-scmutils
<https://www.cs.rochester.edu/%7Egildea/guile-scmutils/>)

The code is too long to paste here, but I have uploaded a copy to my github
<https://github.com/panicz/slayer/blob/master/guile-modules/extra/scmutils/numerics/linear/svd.scm>
.

I have found an ellipsoid-fitting algorithm that leans on this SVD code:
when I run it on a randomly generated set of points lying on an ellipsoid,
the ellipsoid parameters are reconstructed correctly.

The trouble begins when I try to port that code to C. Initially, I have
used a C code for computing SVD that I've found elsewhere (which was based
on the same Fortran code from LINPACK), but I eventually wrote code to
convert Scheme code to C to make sure that both algorithms are identical
(and at this point I am quite confident that they are).

The problem is, that the results of floating-point operations behave
slightly differently in C than in Guile, and these small numerical
differences accumulate, and eventually start making huge differences -- at
least that's something that I've noticed. Moreover, I can quite confidently
say that the results that I get in C are incorrect -- that the *eigenvalues*
(which translate to the radii of the ellipsoid) are very different, the
calculated center of the ellipsoid is slightly different, and that the
*eigenvectors* (which can be interpreted as a rotation of the ellipsoid)
are very similar.

Either way, the ellipsoid isn't reconstructed correctly, because the points
used for reconstruction do not lie near that ellipsoid (unlike in the case
of the C code).

I am really troubled with this behavior -- according to the documentation,
and to the bits of source code that I managed to analyze, Guile uses
exactly the same representation of floating point numbers as the one I use
in C -- namely, the double type. (I only use single-precision floats to
express the input points, but I ran some tests using small integers on
inputs, to make sure, that they are exactly representable -- and the
results were still very different)

Is there a way to make sure that the floating point operations in C behave
identically as those in Guile?


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-10-16  9:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26  8:45 Double-precision floating point arithmetics behaves differently in Guile and in C Panicz Maciej Godek
2021-09-26 11:55 ` Panicz Maciej Godek
2021-09-26 13:16   ` Eli Zaretskii
2021-09-26 16:48     ` Panicz Maciej Godek
2021-09-26 12:57 ` tomas
2021-09-26 19:11 ` Matt Wette
2021-09-27 14:04   ` Panicz Maciej Godek
2021-10-16  9:19     ` Maxime Devos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).