Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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 ( #11134444=note: print w/replies, xml ) Need Help??


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

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).

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

Replies are listed 'Best First'.
Re^2: [OT(C)] float parameter treated as double by callee -- but it fails with FFI
by vr (Curate) on Jun 29, 2021 at 17:03 UTC

    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 !!

      Cheers,
      Rob

      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 ikegami@adaelis.com

        There is one for char (hh).

        The reason it and the one for short (h) exists is purely for symmetry with scanf (which needs to know the type of object at the provided address).

        Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134444]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-09-28 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?