Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ variable promotion during operator

C++ variable promotion during operator

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dan Watt
    wrote on last edited by
    #1

    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?

    C R 2 Replies Last reply
    0
    • D Dan Watt

      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?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      from the ANSI C standard: http://www.scit.wlv.ac.uk/cbook/chap4.usual.conversions.html[^] -c


      Zzzzz...

      ThumbNailer

      D 1 Reply Last reply
      0
      • D Dan Watt

        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?

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        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();

        1 Reply Last reply
        0
        • C Chris Losinger

          from the ANSI C standard: http://www.scit.wlv.ac.uk/cbook/chap4.usual.conversions.html[^] -c


          Zzzzz...

          ThumbNailer

          D Offline
          D Offline
          Dan Watt
          wrote on last edited by
          #4

          Exactly what I was looking for, thanks!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups