kaveh808 / kons-9

Common Lisp 3D Graphics Project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add tests to make sure kons-9 can run in modes suitable for scientific and engineering computing

awolven opened this issue · comments

Get kons-9 running with read-default-float-format set to 'double-float.

The problems arise mostly in the dependencies.

Furthermore, test with (pushnew :3d-vectors-double-floats features) for plugins which require double float vectors and matrices.

I am just curious about the application scenario we are thinking of because it is not clear to me why we want to have more precise floats to render graphics. Do you have a high-level workflow in mind, describing the interaction between KONS-9 and an engineering program? Or any pointer to an ongoing conversation around here? Thanks!

Do you have a high-level workflow in mind, describing the interaction between KONS-9 and an engineering program?

I'm using Kons-9 to visualize the results of Monte Carlo simulations running in the same Lisp image. My code is hungry for floating-point precision because it needs to do long chains of calculations on very small (log-)probability values. I definitely don't want any single-floats in my application-level calculations.

I agree with @awolven that it would be great for Kons-9 to interoperate more easily with this kind of application e.g. be able to load and run in an image that uses double-floats by default. Currently Bad Things Happen when Kons-9 is exposed to double-floats.

I am not questioning at all why scientific computations need double precision but I would like to understand more why KONS-9 should also use double floats for graphic-related computations. So understanding better an example of a workflow would be useful to figure out how to make KONS-9 suitable for scientific and engineering computing while still using faster single floats when it comes to graphic computations.

@foretspaisibles I believe the requirement here is only for Kons-9 to co-exist with code in the same image that uses double-floats. It can still use single-floats internally.

Here's an example that I think represents mine and @awolven's workflows that doesn't work today:

* (setq *read-default-float-format* 'double-float)
* ;; ... write and run some engineering code ...
* (require :kons-9)

Since we will be writing a bunch of engineering code in random files and in the REPL we set *READ-DEFAULT-FLOAT-FORMAT* to DOUBLE-FLOAT so that literals we write like 0.5 will be read as double-float objects. This is simply a user-friendly convenience to avoid writing suffixes like 0.5d0 to get double-floats.

The trouble is that kons-9 compilation breaks when we later try to load it into the same image:

* (setq *read-default-float-format* 'double-float)
* (require :kons-9)
...
; compiling file "/home/luke/git/kons-9/src/kernel/point-origin.lisp" (written 14 NOV 2022 06:52:36 AM):

; file: /home/luke/git/kons-9/src/kernel/point-origin.lisp
; in: DEFUN P-SMOOTH-LERP
;     (ORIGIN.VEC3:LERP KONS-9::P1 KONS-9::P2 (KONS-9::CUBIC KONS-9::F))
; 
; caught WARNING:
;   Derived type of
;   (+ (* -2.0 (* KONS-9::X KONS-9::X KONS-9::X)) (* 3.0 (* KONS-9::X KONS-9::X)))
;   is
;     (VALUES (OR DOUBLE-FLOAT (COMPLEX DOUBLE-FLOAT)) &OPTIONAL),
;   conflicting with its asserted type
;     SINGLE-FLOAT.
;   See also:
;     The SBCL Manual, Node "Handling of Types"

; in: DEFUN P-MIDPOINT
;     (ORIGIN.VEC3:SCALE (ORIGIN.VEC3:+ KONS-9::P1 KONS-9::P2) 0.5d0)
; 
; note: deleting unreachable code
; 
; caught WARNING:
;   Constant 0.5 conflicts with its asserted type SINGLE-FLOAT.
;   See also:
;     The SBCL Manual, Node "Handling of Types"

The reason is that kons-9 source code (and/or its dependencies) includes literals like 0.5 that it assumes will be read into single-float values but in fact won't be due to the customization in this image.

So it seems like kons-9 would ideally either explicitly suffix such literals as 0.5f0 to be agnostic to *READ-DEFAULT-FLOAT-FORMAT* or otherwise somehow (ASDF hook?) ensure that this variable has the necessary value during kons-9 compilation.

Is that a more helpful answer?

(I was going to assert that kons-9 also has problems with being accidentally passed double-float numbers via the API but I couldn't immediately formulate an example so maybe I am imagining that.)

It's not just about reading. You would need to be able to switch between the two, because double floats are wasteful for the majority of things and most people don't care about them. Since points aren't generic you'll also need to switch between libraries.

I made a separate issue #180 about the specific issue that kons-9's requirement that *read-default-float-format* be single-float is not documented (and potentially unfortunate.)

If that issue captures everything that is actionable here then perhaps we could close this issue.

It would be good to be able to have the user be able to specify if they want single or double floats when they are loading our system. There are definitely use cases for both.

Let''s figure out how in #180

I'm using Kons-9 to visualize the results of Monte Carlo simulations running in the same Lisp image.

@lukego I'd love to hear more about your use of the system. Maybe post about it in discussion #78 ?

@foretspaisibles I believe the requirement here is only for Kons-9 to co-exist with code in the same image that uses double-floats. It can still use single-floats internally. […]

Thank you @lukego that's a very helpful description of what you would like to do. I do not have experience with automated testing for Slime/Emacs or Sly/Emacs but we could start with a plain lisp test.

Yet that could be interesting to learn about UX-testing in Emacs for the future.