Re: more lobbying

new topic     » goto parent     » topic index » view thread      » older message » newer message

Mic writes:

> * Make it possible to pass floats to C functions
> (type-casting, eh?).

Currently only C doubles (64-bit) are fully supported,
not C floats (32-bit).  Floats are not used in C nearly as
much as doubles. I intend to support floats fully in the
next release. Until then you can use the following
workaround that David Guy and I discovered last January.

The rest is a message that David Guy posted to this list...

------------------------------------------

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

<correct answer>

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu