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#
  4. My computer can't do basic arithmetic (or I'm doing something stupid)

My computer can't do basic arithmetic (or I'm doing something stupid)

Scheduled Pinned Locked Moved C#
question
6 Posts 5 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.
  • A Offline
    A Offline
    ajtunbridge
    wrote on last edited by
    #1

    float aspectRatio = 1280 / 1024;
    Console.WriteLine(aspectRatio.ToString());

    RESULT? 1

    float aspectRatio = 1280 / 800;
    Console.WriteLine(aspectRatio.ToString());

    RESULT? 1

    float aspectRatio = 1280 / 500;
    Console.WriteLine(aspectRatio.ToString());

    RESULT? 2 Any idea why this might be?

    B F A B J 5 Replies Last reply
    0
    • A ajtunbridge

      float aspectRatio = 1280 / 1024;
      Console.WriteLine(aspectRatio.ToString());

      RESULT? 1

      float aspectRatio = 1280 / 800;
      Console.WriteLine(aspectRatio.ToString());

      RESULT? 1

      float aspectRatio = 1280 / 500;
      Console.WriteLine(aspectRatio.ToString());

      RESULT? 2 Any idea why this might be?

      B Offline
      B Offline
      buchstaben
      wrote on last edited by
      #2

      try

      float aspectRatio = 1280f / 500f;

      what you're doing is calculating with integers and assigning the result to float. you need to calculate with floats.

      1 Reply Last reply
      0
      • A ajtunbridge

        float aspectRatio = 1280 / 1024;
        Console.WriteLine(aspectRatio.ToString());

        RESULT? 1

        float aspectRatio = 1280 / 800;
        Console.WriteLine(aspectRatio.ToString());

        RESULT? 1

        float aspectRatio = 1280 / 500;
        Console.WriteLine(aspectRatio.ToString());

        RESULT? 2 Any idea why this might be?

        F Offline
        F Offline
        Frank Horn
        wrote on last edited by
        #3

        The compilers understands the right side expressions to be integers. You only get a decent result with float aspectRatio = (float)1280 / (float)500;

        1 Reply Last reply
        0
        • A ajtunbridge

          float aspectRatio = 1280 / 1024;
          Console.WriteLine(aspectRatio.ToString());

          RESULT? 1

          float aspectRatio = 1280 / 800;
          Console.WriteLine(aspectRatio.ToString());

          RESULT? 1

          float aspectRatio = 1280 / 500;
          Console.WriteLine(aspectRatio.ToString());

          RESULT? 2 Any idea why this might be?

          A Offline
          A Offline
          ajtunbridge
          wrote on last edited by
          #4

          I see. Thanks to you both.

          1 Reply Last reply
          0
          • A ajtunbridge

            float aspectRatio = 1280 / 1024;
            Console.WriteLine(aspectRatio.ToString());

            RESULT? 1

            float aspectRatio = 1280 / 800;
            Console.WriteLine(aspectRatio.ToString());

            RESULT? 1

            float aspectRatio = 1280 / 500;
            Console.WriteLine(aspectRatio.ToString());

            RESULT? 2 Any idea why this might be?

            B Offline
            B Offline
            blackjack2150
            wrote on last edited by
            #5

            The result of an arithmetic operation on two integers is an integer. To get the float value, you must do explicit casting.

            1 Reply Last reply
            0
            • A ajtunbridge

              float aspectRatio = 1280 / 1024;
              Console.WriteLine(aspectRatio.ToString());

              RESULT? 1

              float aspectRatio = 1280 / 800;
              Console.WriteLine(aspectRatio.ToString());

              RESULT? 1

              float aspectRatio = 1280 / 500;
              Console.WriteLine(aspectRatio.ToString());

              RESULT? 2 Any idea why this might be?

              J Offline
              J Offline
              John_Adams
              wrote on last edited by
              #6

              Hi, You need to type cast the expression to float like the following: float aspectRatio = (float)1280 / 1024; MessageBox.Show(aspectRatio.ToString()); Or float aspectRatio = (float)1280 / 500; MessageBox.Show(aspectRatio.ToString()); This will fix the issue.

              Regards, John Adams ComponentOne LLC

              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