C++ variable promotion during operator
-
I am designing a scripting engine for use in a student project, that has similar syntax to C++ but not quite all the power. I have recently come to one small problem. When you have an operation, like a+b, there needs to be a return type from that operation. (though this does have a lot to do with my engine, it also is a question as to how C++ works, since I havn't really enountered any material on this subject - maybe I'm just not looking hard enough). Say we have: float f = 1+1.0; f gets the value 2.0 since we know we are storing to a float. But, if I am not going to store to float, but rather do something like cout << 1+1.1 << endl; (not in my code, but in C++) That gets the value of 2.1 (tested in C++), but how does the compiler know to promote to a float? Seems simple enough, but the engine is designed so that types can be added, so I want to know whats the best way to know how to promote a variable to another type during an operator (specifically, a binary operator)? I guess a better way of putting it would be "How can you determine what the resulting type will be from an operator that is done on two different types"? Will the first operand have precidence, or the second?
-
I am designing a scripting engine for use in a student project, that has similar syntax to C++ but not quite all the power. I have recently come to one small problem. When you have an operation, like a+b, there needs to be a return type from that operation. (though this does have a lot to do with my engine, it also is a question as to how C++ works, since I havn't really enountered any material on this subject - maybe I'm just not looking hard enough). Say we have: float f = 1+1.0; f gets the value 2.0 since we know we are storing to a float. But, if I am not going to store to float, but rather do something like cout << 1+1.1 << endl; (not in my code, but in C++) That gets the value of 2.1 (tested in C++), but how does the compiler know to promote to a float? Seems simple enough, but the engine is designed so that types can be added, so I want to know whats the best way to know how to promote a variable to another type during an operator (specifically, a binary operator)? I guess a better way of putting it would be "How can you determine what the resulting type will be from an operator that is done on two different types"? Will the first operand have precidence, or the second?
from the ANSI C standard: http://www.scit.wlv.ac.uk/cbook/chap4.usual.conversions.html[^] -c
Zzzzz...
-
I am designing a scripting engine for use in a student project, that has similar syntax to C++ but not quite all the power. I have recently come to one small problem. When you have an operation, like a+b, there needs to be a return type from that operation. (though this does have a lot to do with my engine, it also is a question as to how C++ works, since I havn't really enountered any material on this subject - maybe I'm just not looking hard enough). Say we have: float f = 1+1.0; f gets the value 2.0 since we know we are storing to a float. But, if I am not going to store to float, but rather do something like cout << 1+1.1 << endl; (not in my code, but in C++) That gets the value of 2.1 (tested in C++), but how does the compiler know to promote to a float? Seems simple enough, but the engine is designed so that types can be added, so I want to know whats the best way to know how to promote a variable to another type during an operator (specifically, a binary operator)? I guess a better way of putting it would be "How can you determine what the resulting type will be from an operator that is done on two different types"? Will the first operand have precidence, or the second?
Neither the first nor the second, but the you may make the precidence according to the type : in fact, memory sapce allocated for float is bigger than memory for int. According to this criteria, make a previdence list (like : double,float,long int,int,char) and promote all your operand to the best ranked in this list. ~RaGE();
-
from the ANSI C standard: http://www.scit.wlv.ac.uk/cbook/chap4.usual.conversions.html[^] -c
Zzzzz...