Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

[OT(C)] float parameter treated as double by callee -- but it fails with FFI

by vr (Curate)
on Jun 29, 2021 at 13:44 UTC ( #11134440=perlquestion: print w/replies, xml ) Need Help??

vr has asked for the wisdom of the Perl Monks concerning the following question:

Documentation for the libtiff tag setter function says that TIFFTAG_XRESOLUTION tag must be passed as float value. Code examples I see elsewhere (e.g. XS code for Graphics::TIFF), tutorials/examples on the web, SO answers, etc., as well as my own C tests show that, yes indeed, resolution is to be passed as float, and it all works as expected.

However, reading source, I see that callee treats it as double (only to convert it to float a few lines below). First surprise is why the heck and how it works. Even if values on stack are aligned to more than 4 bytes, these are not integers, and packed float value is not a substring of packed double value, is it?

I dug it all up, because I was writing FFI binding for aforementioned tag setter function. I tried different modules: FFI::Platypus, FFI::Raw (though both are libffi based), and they fail to set resolution tag, if I describe parameter as "float". Rather, tag is set to 0, regardless of parameter, function call itself doesn't fail. But, if parameter is described as "double", tag is set as expected (I'll post SSCCE if required).

So, looks like no problem, I have working code, which is temporary prototype anyway, but I'm confused. Even if I feel brave enough to ask libtiff people "maybe you have a bug?", they'd say "all code using libtiff in whole world works fine. What's FFI::Platypus and why would we care?"

  • Comment on [OT(C)] float parameter treated as double by callee -- but it fails with FFI

Replies are listed 'Best First'.
Re: [OT(C)] float parameter treated as double by callee -- but it fails with FFI
by syphilis (Archbishop) on Jun 29, 2021 at 14:45 UTC
    I see that callee treats it as double (only to convert it to float a few lines below).

    I assume it simply casts the double to a float ?
    I don't see anything particularly puzzling about that.
    That's exactly what happens in TIFF.xs, too, with f = SvNV(ST(2));
    "f" is a float, but SvNV(ST(2)) will return an NV (ie a double or long double or __float128, depending upon perl's nvtype).


      Exactly, in TIFF.xs double is simply cast to float, no issues with that. My confusion was about line 337, where double is expected as argument. But, what I just found in plain Wikipedia:

      Another consideration is the default argument promotions applied to the unnamed arguments. A float will automatically be promoted to a double. Likewise, arguments of types narrower than an int will be promoted to int or unsigned int. The function receiving the unnamed arguments must expect the promoted type.

      That answers my question about libtiff code. And, perhaps implicitly, addresses the question about FFI: for variadic functions, promoted args types must be specified (i.e. not float, but double). OK, now I'm educated:)

        A float will automatically be promoted to a double

        I wasn't aware of this, either.
        I've eventually located this info in K&R (2nd Edition) in section A7 - "A7.3.2 Function Calls".
        Thanks for spreading the education !!


        That's why there's no printf length modifier for char, float or double.

        signed char c = 123; int i = 123; float f = 123; double d = 123; printf("%d\n", i); // ok printf("%d\n", c); // ok, even though %d expects int. printf("%f\n", d); // ok printf("%f\n", f); // ok, even though %f expects double.

        (Not sure why there's one for short since they are guaranteed to be no larger than an int and unsigned int respectively.)

        Seeking work! You can reach me at

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11134440]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2022-10-02 10:19 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (8 votes). Check out past polls.