Something really dumb with return values - objective-c

I'm doing something really dumb, and I don't see it.
I've got an object doc with a method:
-(float) currentOrient
{
return 50.5;
}
In another object, I call:
-(void) showPage
{
float rot2=0;
rot2 = [doc currentOrient] ;
NSLog(#"SP rotation is %.2f", rot2);
}
However, the output is :
SP rotation is 1112145920.000000
No, one question is "Why is the %2f not formatting correctly?" But the more confusing question is "Where is that number coming from?" Yes, I've walked through it with a debugger, the value of rot DOES change from the garbage it starts with. and that number DOES appear to be consistent.
Clearly something really dumb is going on...

It sounds like the showPage method doesn't know right return type for currentOrient, so it's interpreting the value returned as an int and casting that nonsensical int to a float. Are you getting any warnings? Are you sure you're importing the header for currentOrient correctly? Is the currentOrient method declared correctly?

I can answer the first question:
Why is the %2f not formatting correctly?
Because it ought to be %1.2f to round to two decimal places (which I believe is what you're trying to achieve?)
And guess at the second:
Do you have a property named rot in the code? Other than that... shrug... I don't know - I'm assuming you've simplified the example to post on SO, have you taken out other code that may be relevant? Based on the information you've provided everything should be ducky.
On a side note: When I hit bugs like this I go do something physical. Usually when I come back my head is clear and I find the problem immediately. You might want to give that a try too! :D

Related

Converting std::int to System::Single

Apologies if there's an answer out there already; but all I seem to be getting is a bunch of "I want to turn my 1 into a 1.0" chaff from my Google searches.
First things first. No, I'm not talking about a simple Convert::ToSingle() call. Rather, I need to convert the representation of the data to a System::Single.
So in other words, I'd like to take int myInt = 1065353216;, and the result should be something like 1.000. I know the pure c++ method would be something like float myFloat=*(float *)&myInt;;but I need the managed version.
Thanks in advance for your help.
If you're in C++/CLI, you can do it the same way as you do in C++: float myFloat=*(float*)&myInt;
In pure managed-land, there are built-in methods to do this for double & Int64 (DoubleToInt64Bits and Int64BitsToDouble, but not for single & Int32. However, if you look at the implementation of those methods (MS Reference Source), you'll see that they're doing the exact same thing as you have listed, so that's also the managed way to do it. The only difference is if you do it in C#, you have to tag the method as unsafe.

Uniscribe and Text Outlines

Can I use the output from ScriptShape/ScriptItemize in Uniscribe to get the char codes and pass the char codes to GetGlyphOutline to get the beziers?
Does Uniscribe have a call to get the outlines directly?
For anyone wondering the same.
The short answer: Yes. It works.
Uniscribe returns a WORD whereas the GetCharacterPlacement one gives out an LPCSTR though, so just look at that, should be trivial.
Nothing surprising though, this was just to confirm before I spend my time on it :)

Format-Specifiers Syntax Error?

i am having a little trouble with printf specifiers...so before asking you guys i read almost everything onC++Reference page, but couldnt fix the problem, and since i am new at c i cant even understand the problem, its most likely a syntax error but i can't find it...
for(i = 1; i <= 10; i++) {
printf("\n%d.%s%n",i,names[i-1],offset);
printf("%*s%.2f TL",10-offset," ",prices[i-1]);
}
so basically i have this code to print a list, and i want the prices to start from the same column.
For e.g:
water 1.00
oj 1.00
and the logic behind my code (incase it's not obvious, i can't tell if it is) is:
print id number and name, count how many chars we've written so far and assign it to offset.
print (starting column of price list)-offset spaces before price
once i couldn't get the result i want, i checked and found out that offset is 3 for all names which is not the case(and no value is assigned to offset before this procedure).
Thanks for any kind of help !
PS: This is a practice code just to get better at using specifiers efficiently.
edit:
so i did this :
for(i=1;i<=10;i++)
{
printf("%d.%s%n",i,names[i-1],&offset);
printf("%*s%.2f TL\n",10-offset," ",prices[i-1]);
}
but what i get as a result is huge empty black command screen.
The %n format specifier requires a pointer. Your code is missing the & operator for offset:
printf("\n%d.%s%n",i,names[i-1],&offset);
The good ol' C interface doesn't know what types you supply to printf so it doesn't complain and happily reads the 4 byte integer value of offset on the stack as a memory location -> core dump.
Actually, g++ with -Wall does warn. So
hd1 has a point here because C++ output is type safe (even though it's a pain);
Heed thy warnings.
When you use %n in a printf format, the corresponding parameter must be a pointer. printf will store the information in the int you point it to.
Assuming you've declared int offset somewhere, you should use &offset as the last argument in your printf call.
While we're here, allow me to comment on this excerpt:
printf("\n
ARGH NO! Newline is a terminator. It goes at the end of a line, not the beginning.
so i did this :
for(i=1;i<=10;i++)
{
printf("%d.%s%n",i,names[i-1],&offset);
printf("%*s%.2f TL\n",10-offset," ",prices[i-1]);
}
but what i get as a result is huge empty black command screen.
edit: Can you guys try this and tell me if you get normal results? I can't understand the mistake occuring, so i can't get past it...Maybe some other examples will lead me to where is the mistake.

"Expected unqualified-id" in #define statement

I'm trying to simplify my code by using #define statements. This is because it contains a lot of repetitive "chunks" of code that cannot be repeated using the obvious alternative, functions, because in these chunks, variables need to be declared like you'd do in a #define statement, e.g. #define dostuff(name) int name##Variable;.
Code
#define createBody(name,type,xpos,ypos,userData,width,height) b2BodyDef name##BodyDef;\
name##BodyDef.type = type==#"dynamic"?b2_dynamicBody:b2_staticBody;\
name##BodyDef.position.Set(xpos,ypos);\
name##BodyDef.userData = userData;\
name=world->CreateBody(&name##BodyDef);\
b2PolygonShape name##shape;\
name##shape.SetAsBox(width/ptm_ratio/2,height/ptm_ratio/2);
... and applying that in the following:
createBody(block, #"dynamic", winSize.width*5/6/ptm_ratio, winSize.height*1/6/ptm_ratio, ((__bridge void*)blockspr), blockspr.contentSize.width, blockspr.contentSize.height)
// error appears there: ^
Now my point is that everything's working great, no errors, except a single one that's freaking me out:
Expected unqualified-id
which points at the first bracket in ((__bridge ..., as indicated. (That argument gets passed via the userData argument to createBody.)
I know this code is nowhere near simple, but since everything else is working, I believe that an answer must exist.
This is my first question on SO, so if there's anything unclear or insufficient, please let me know!
I'm trying to simplify my code by using #define statements.
This sounds an alarm in my mind.
Break this down into functions. You said you can't. I say you can.
Notice that your macro here:
createBody(name,type,xpos,ypos,userData,width,height);
It has exactly the same syntax as a C function. So you've already created a function, you only declared it as a macro. There's no reason why you couldn't rewrite it as a function (C or Objective-C doesn't matter). You do not need to give each body its own name, instead you could store them in a dictionary (careful though because Box2D takes ownership of the bodies).

Objective-C, Expression result unused, from some old code?

I've getting the following warning Expression result unused
I've inherited the code from my predecessor and I've no idea how to fix this?
Obviously syntax has changed, any ideas ?
static float lowValue;
static float highValue;
- (void) calculateHighLow{
highValue; //here
lowValue; // and here
Simply delete those two lines, they have no purpose.
Obviously those two lines don't do anything so you can just remove them.
If they aren't being used anywhere you can just delete the lines, or you can simply just decide to deal with the warning (though not a good idea). You can also comment out the code, in case you figure out that might need them someday.