Re: Passing Floats by Value
- Posted by David Guy <dguy at MINDSPRING.COM> Jan 20, 2000
- 500 views
On Wed, 19 Jan 2000 21:13:26 -0500, Robert Craig <rds at ATTCANADA.NET> wrote: >David Guy writes: >> How do I pass a 4-byte floating point value from >> Euphoria to a C routine? > >Euphoria only supports passing floating-point atoms to C routines >in the form of C doubles (8 bytes). From my experience, >it's extremely rare that a C routine will pass float arguments >(4-bytes), and even if it did, I believed (until now) that most, >if not all, C compilers automatically promote float arguments >to double before passing them. I suppose I could support >passing atoms as C floats in the next release. > >Meanwhile, here's a kludge that you can try. >- convert each argument, a, as: > a = bytes_to_int(atom_to_float32(a)) > >- define the C function in Euphoria as if it takes C_INT's > as arguments > >- pass each argument, a, as a C_INT You're kludge works! :) If I "link" to the routine in either of the following ways: constant m_myFunc = define_c_proc(m_DLL, "_myFunc@8", {C_FLOAT, C_FLOAT}) or constant m_myFunc = define_c_proc(m_DLL, "_myFunc@8", {C_INT, C_INT}) and then call the routine like so: c_proc(m_myFunc, {bytes_to_int(atom_to_float32(1.0)), bytes_to_int(atom_to_float32(1.0))} ) the following prints out: 1.000000 1.000000 As to supporting the passing of atoms as C floats,...YES please do! It would be nice if when a float was specified, a float was passed. :) It can't be that difficult for Euphoria to convert an atom down to a float before passing it, and I'm sure it would be much faster, and certainly cleaner and more straight forward, than the above kludge. Thanks for the help, David Guy