how do I pass multiple float arguments to an inlet?

Feb 2, 2008 at 8:19am

how do I pass multiple float arguments to an inlet?

Hi, I am trying to pass multiple floats as aguguments to an intlet. This is how I am trying
(I am not using a list because I want to have the “myMessage” selector and I have read somewhere that lists have to start with a number):

void myMethod( t_myObject *x, double val1, double val2, double val2, double val4);

class_addmethod(c, (method)myMethod , “myMessage” , A_FLOAT , A_FLOAT , A_FLOAT , A_FLOAT, 0);

void myMethod( t_myObject *x, double val1, double val2, double val2, double val4)
{
post(” %f + %f +%f +%f = %f”, val1, val2, val3, val4, val1+val2+val3+val4);
}

then in my patch I use a messagebox with myMessage 1.0 2.0 3.0 4.0

and in the max window, instead of getting:
1.0 + 2.0 + 3.0 + 4.0 = 10.0

I get:

0.0000000 + -324.00000 +1000000.000000+7381388.000000 = 8381064.000000

I have my function prototype as double as mentionned in the “writing max externals”. What am I doing wrong?

Thanks

#35623
Feb 2, 2008 at 8:57am

Without trying, I believe you need to use the A_GIMME argument in class_addmethod like so:

class_addmethod(c, (method)myMethod, “myMessage”, A_GIMME,0);

You method then needs to be formatted like:

void myMethod(t_myObject *x, t_symbol *s, long ac, t_atom *av)
{
//parse the atoms in av and check if they are floats.
}

Btw I agree that it would be more intuitive if there would be an option to specify the types of all arguments individually.

Mattijs

#121617
Feb 2, 2008 at 9:35am

One more thing (without trying out, as well…): you have doubles in
your argument list. However, you’re treating them as floats in the post
message. For double-precision floating point objects, I believe you
should use %lf (‘long float’) instead of simply saying %f. As they have
different allocation size, this mistyping is likely to cause your problem.

Cheers,
Adam

________________
Siska Ádám
+36 (70) 207-63-85

http://www.sadam.hu/

tirilo wrote:
> Hi, I am trying to pass multiple floats as aguguments to an intlet. This is how I am trying
> (I am not using a list because I want to have the “myMessage” selector and I have read somewhere that lists have to start with a number):
>
> void myMethod( t_myObject *x, double val1, double val2, double val2, double val4);
>
> …
>
> class_addmethod(c, (method)myMethod , “myMessage” , A_FLOAT , A_FLOAT , A_FLOAT , A_FLOAT, 0);
>
> …
>
> void myMethod( t_myObject *x, double val1, double val2, double val2, double val4)
> {
> post(” %f + %f +%f +%f = %f”, val1, val2, val3, val4, val1+val2+val3+val4);
> }
>
>
>
> then in my patch I use a messagebox with myMessage 1.0 2.0 3.0 4.0
>
> and in the max window, instead of getting:
> 1.0 + 2.0 + 3.0 + 4.0 = 10.0
>
> I get:
>
> 0.0000000 + -324.00000 +1000000.000000+7381388.000000 = 8381064.000000
>
>
> I have my function prototype as double as mentionned in the “writing max externals”. What am I doing wrong?
>
>
>
> Thanks
>
>
>
>

#121618
Feb 2, 2008 at 12:51pm

If you always have three floats, you can use the signature you suggested. If the number of floats is variable, you need to use A_GIMME.

It is *always* important to state what platform you’re testing on. Why make people guess?

I am pretty sure that all Mac OS compilers (ie gcc) treat %f inside a sprintf format as doubles and pass all floats in 64-bit registers. I’m pretty sure that Windows compilers don’t.

I have made it a habit to *always* specify %lf in sprintf formats and to typecast parameters to double if there is any doubt about their size. That is in my experience the safest, most cross-platform reliable way to be sure that things work.

In any case, your output is screaming “float-double mismatch”.

#121619
Feb 2, 2008 at 1:50pm

Quote: Peter Castine wrote on Sat, 02 February 2008 13:51
—————————————————-
> If you always have three floats, you can use the signature you suggested.

It does?! Hey, cool. I didn’t know.

Mattijs

#121620
Feb 2, 2008 at 5:38pm

Thank you all for your answers and sorry for not mentioning my platform. I am on a Mac powerbook G4 running osX 10.4.11.

So I actually had tried %lf with no luck I get the same results Both with post and cpost. The only way I get proper results is by using A_GIMME as Mattijs suggested.
The thing is that I do always have 4 floats values in so I would much rather use my original signature as Peter says, and not do the atom parsing if I don’t have to. But I still don’t get it. Anyone had luck using multiple float signatures?

Thanks,
Tirilo

ps Peter Thanks for mentionning that I should always use %lf and doubles and also cast to double when in doubt. (btw Does it mean that I shall do that for every method that takes A_FLOAT arguments? ie never use floats but always doubles?)

#121621
Feb 2, 2008 at 6:01pm

On 2008 Feb 2, at 11:38 AM, tirilo wrote:

> So I actually had tried %lf with no luck I get the same results Both
> with post and cpost. The only way I get proper results is by using
> A_GIMME as Mattijs suggested.
> The thing is that I do always have 4 floats values in so I would
> much rather use my original signature as Peter says, and not do the
> atom parsing if I don’t have to. But I still don’t get it. Anyone
> had luck using multiple float signatures?

Hi,
If you notice, Peter said “3 floats”. 4 floats, which is actually
passed as 4 doubles, adds up to too many bits to be passed as args
like this. Thus, you will need to use A_GIMME for 4 floats.

best,
Tim

#121622
Feb 2, 2008 at 6:41pm

Thanks! So actually for some reason the limitaion on my machine is actually 2 floats and not 3. But it works with 2 and now I understand.

Thanks.

#121623
Feb 2, 2008 at 7:48pm

Quote: Timothy Place wrote on Sat, 02 February 2008 19:01
—————————————————-

> If you notice, Peter said “3 floats”. 4 floats, which is actually
> passed as 4 doubles, adds up to too many bits to be passed as args
> like this. Thus, you will need to use A_GIMME for 4 floats.
—————————————————-

Actually, this is a point where science ends and voodoo takes over.

Last time I read the documentation (caveat: it’s been a while), you could bind up to 7 typed arguments to a message, no ifs/ands/buts. However, the fact is that the last time this is reported to have worked unconditionally was on 68k architectures.

It has been reported on this list many times over the last 10 years that argument lists **with mixed types** do not work correctly with more than three arguments (>3, >=4). [but seen note below]. So (A_FLOAT, A_FLOAT, A_FLOAT, 0) and (A_LONG, A_SYM, A_FLOAT, 0) and other triples are OK. But with four arguments, mixing types breaks (ie: A_LONG, A_FLOAT, A_FLOAT, A_LONG, 0). In my experience four floats (A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0) does work. At least used to.

When typed lists don’t work, you have to fall back to A_GIMME and parse yourself. That’s the long and short of it.

NOTE: The above applies to instantiation arglists. I’ve not tried this with message arglists. I don’t see why there should be a difference, but who knows?

Alternately, it may be that since XCode there are even more limitations to arglists than CodeWarrior, and that would explain the problems seen here.

TTBOMK, none of this is documented in the SDK. The fact that it’s been discussed online makes the information nothing more than folklore. The parts that DDZ and JKC have contributed are elevated to authoritative folklore, but if it’s not in the documentation it’s still folklore.

Again, I would hope that some time into Max 5 these gotchas would be included in a Grand Unified SDK. I still hope there will be a quick interim SDK (“Differences Only” is fine by me if that gives an early release).

Also, I’d still make a habit of %lf in sprintf formats. Sprintf is evil. Take no chances.

#121624
Feb 2, 2008 at 10:32pm

On 2 Feb 2008, at 18:01, Timothy Place wrote:

> If you notice, Peter said “3 floats”. 4 floats, which is actually
> passed as 4 doubles, adds up to too many bits to be passed as args
> like this.

Four shalt thou not count, neither count thou two, excepting that thou
then proceed to three. Five is right out.

– N.

Nick Rothwell / Cassiel.com Limited
http://www.cassiel.com
http://www.myspace.com/cassieldotcom
http://www.last.fm/music/cassiel
http://www.reverbnation.com/cassiel
http://www.linkedin.com/in/cassiel
http://www.loadbang.net

#121625
Feb 3, 2008 at 9:15am

> The parts that DDZ and JKC have contributed are elevated to
> authoritative folklore, but if it’s not in the documentation it’s
> still folklore.

I like the sound of your poetry on this Sunday morning Peter.

> Again, I would hope that some time into Max 5 these gotchas would
> be included in a Grand Unified SDK.

Ah, it sounds like a dream… I still do some voodoo with the help
of a couple of kind developers to write ethically and cleanly to a
buffer~…

pa from a sunny West Yorkshire… oups, I wrote this email for too
long it is now raining ;-)

#121626

You must be logged in to reply to this topic.