Converting std::int to System::Single - c++-cli

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.

Related

cmake: add defines of data types handled as string

Standard C
I need to add data types to project because GNU do not understand some C51 data types. Example need a BYTE types as:
#define BYTE unsigned char
Have tried following examples:
add_definitions(-DBYTE=\"unsigned char\")
add_definitions(-DBYTE="unsigned short")
add_definitions(-DBYTE="\"unsigned long\"")
Some other ideas?
thanks :-)
This should work:
add_definitions("-DBYTE=unsigned char")
Antonio's suggestion is pretty good. But in case you are looking for using the defines at configure time, you can use the approach mentioned in your question.
To add more information, you can use something like:
set(MYDEFINES -DVAR1=value1 -DVAR2=value2)
add_definitions(${MYDEFINES})
add_definitions() accepts a list which is very useful sometimes. You don't have to convert to a string.

Why do I use "parse"

What is the reasoning for parsing an integer? For instance, Integer.Parse('variable'.text)
I see this a lot and while manipulating data for a calculator I am building I found that Val('variable'.text) was all I need to use "numeric" values.
So, my question is how does Integer.Parse() help me with regards to calculators?
Thanks!
I found that "Val('variable'.text)" was all I need
If that's the case then go ahead and use Val(). But be aware that it behaves differently than .Parse() (or, often preferably, .TryParse()) methods.
For example, what do you want to do if the user inputs "123 isn't 456"? Val() will (I think) return:
123 As Double
Or how about the input "123 456"? That would be:
123456 As Double
Do you want it to be a Double? Do you want it to throw an error because it's not purely numeric? Something else? The behavior you want should be reflected in the code you write. Use Val() for one set of behaviors, .Parse() for another.

Is it possible to parse a mathematical expression by using #define?

I want to make a scientific calculator in which the user enters something like 3+4*(3-5)/23 and then the calculator can return the value.
Now I'm trying to find a way to parse a string of mathematical expression. I know that there are some built parsers and algorithms but I want to know whether it's possible by using #define method.
Basically, I want to use the #define to literally remove the # and " " in a string and make it look like an expression that can be evaluated. At this stage, I won't use unknown variables like x or 3*k or a*b/c. All will be numbers and operators like 3+4 and 32 that can be directly evaluated by the compiler. Here is what I want to write in #define:
#define eval#"(x)" x
In the above code, eval is just a signal of parsing and the #"x" is the actual string that need to parse and x is a mathematical expression. After the translation, only x will remain. For example, if I write
double result = eval#"(3+4)";
the compiler will read
double result = 3+4;
(according to my understanding of #define). However, the code does not work. I suspect that the quotation marks confuse the compiler and cause the code to break. So my question is: can anyone come up with a solution using #define?
This is not possible with the preprocessor, no string manipulation besides concatenation supported.
Why would you need the #"x" syntax anyways? You can just put the expression right there in the code.
People are right, you cannot do it in direct way, however if you very want macro:
#define eval(x) [[[NSExpression expressionWithFormat:x] expressionValueWithObject:nil context:nil] doubleValue]
double result = eval(#"3+4");
#define is an invocation of the C preprocessor, which is not capable of this kind of manipulation. It almost sounds like you're trying to define an Objective-C macro that would do the same kind of thing as a LISP macro, but that's not possible. Why don't you tell us what the original problem is that you're trying to solve... I think we can probably come up with an easier way to do what you're trying to do.

objective-c equivalent to c++ numeric_limits::max()

I've gotten used to utilizing the numeric_limits part of the C++ STL for initializing numeric types (int,float, etc.) to their largest possible value.
I.e. int i=numeric_limits::max()
Is there an equivalent to this in objective-c? I've seen using INT_MAX and FLT_MAX in google searches, but it seems like there should be a better way.
There is:
NSIntegerMax, NSIntegerMin, CGFLOAT_MAX etc.
These are sufficient for getting the numeric limits.

Something really dumb with return values

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