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. How do I cast a double to an int?

How do I cast a double to an int?

Scheduled Pinned Locked Moved C#
databasehelpquestionxml
21 Posts 7 Posters 11 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.
  • J Offline
    J Offline
    Justiin1265
    wrote on last edited by
    #1

    Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

    L D A A L 6 Replies Last reply
    0
    • J Justiin1265

      Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You're returning a double. Try converting that to an Int32.

      Justiin1265 wrote:

      try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception.

      How about "If (totalUnits > 0)", since you're not allowed to divide by nothing?

      I are Troll :suss:

      J 1 Reply Last reply
      0
      • J Justiin1265

        Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

        D Offline
        D Offline
        David1987
        wrote on last edited by
        #3

        Please use <pre> tags

        J 1 Reply Last reply
        0
        • L Lost User

          You're returning a double. Try converting that to an Int32.

          Justiin1265 wrote:

          try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception.

          How about "If (totalUnits > 0)", since you're not allowed to divide by nothing?

          I are Troll :suss:

          J Offline
          J Offline
          Justiin1265
          wrote on last edited by
          #4

          That's what I am cofused on. I thought I was inserting the formula into the conversion statement. What is the correct way, b/c the way I am trying to do it gives me an error? Justin

          L 1 Reply Last reply
          0
          • D David1987

            Please use <pre> tags

            J Offline
            J Offline
            Justiin1265
            wrote on last edited by
            #5

            I know I'm sorry, someone told me about it last time. I poseted this in a rush and forgot. Justin

            A 1 Reply Last reply
            0
            • J Justiin1265

              Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              On which line does the compiler show the error? My guess is it goes wrong at

              return Refurb_Rate;

              as the method return type is Int32. If you were to change the declaration of Refurb_Rate from Double to Int32 then the problem would go away. I'm not sure what you are trying to do in the line

              Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100);

              It doesn't make much sense as the result of the expression on the RHS is already Int32 making the explicit conversion redundant. Alan.

              L J 2 Replies Last reply
              0
              • J Justiin1265

                I know I'm sorry, someone told me about it last time. I poseted this in a rush and forgot. Justin

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #7

                Pro-tip: you can still edit your question to fix it.

                Flummery:

                This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                1 Reply Last reply
                0
                • J Justiin1265

                  That's what I am cofused on. I thought I was inserting the formula into the conversion statement. What is the correct way, b/c the way I am trying to do it gives me an error? Justin

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You can check whether totalUnits is zero, before attempting a divide;

                  Refurb_Rate = 0;
                  if (totalUnits > 0)
                  Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100);

                  You're returning a double, called Refurb_Rate, where the method is expected to return an int, on the last line;

                  return Convert.ToInt32(Refurb_Rate);

                  Hope this helps :)

                  I are Troll :suss:

                  1 Reply Last reply
                  0
                  • A Alan N

                    On which line does the compiler show the error? My guess is it goes wrong at

                    return Refurb_Rate;

                    as the method return type is Int32. If you were to change the declaration of Refurb_Rate from Double to Int32 then the problem would go away. I'm not sure what you are trying to do in the line

                    Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100);

                    It doesn't make much sense as the result of the expression on the RHS is already Int32 making the explicit conversion redundant. Alan.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    it will also result in a zero value most of the time. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    1 Reply Last reply
                    0
                    • J Justiin1265

                      Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #10

                      I have a program for work that uses a formula to calculate the refurb on a unit (parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that, but still get the error "Cannot implicitly convert type double to int. An explicit conversion exists (are you missing a cast?)" What am I doing wrong? Here's some of my code to look at:

                      private int GetRefurbRate()
                      {
                      string sql = "";
                      double Refurb_Rate;
                      int totalRefurb = 0;
                      int totalUnits = 0;
                      string error_msg = "";

                      sql = "SELECT COUNT(rp.repair\_ord) " +
                          "FROM " + schema + ".repair\_part rp " +
                          "WHERE rp.repair\_ord = '" + repair\_ord + "' ";
                      while (true)
                      {
                          if (!myDb.RunSql(sql, true))
                          {
                              error\_msg = "DBError for getting Refurb Rate";
                              break;
                          }
                          if (myDb.dbRdr.HasRows)
                          {
                              if (myDb.dbRdr.Read())
                              {
                                  try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception.
                                  {
                                      Refurb\_Rate = Convert.ToInt32( totalRefurb / totalUnits \* 100); //This is where I try to perform the cast.
                                  }
                                  catch (Exception e)
                                  {
                                      Console.WriteLine(e);
                                  }
                              }
                      
                              //int Refurb\_Rate = Convert.ToInt32(Refurb\_Rate);
                          }
                      
                          break;
                      }
                      myDb.dbRdr.Close();
                      
                      if (error\_msg != String.Empty)
                      {
                          MessageBox.Show(error\_msg, "Get Refurb Rate",
                          MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                      }
                      
                      return Refurb\_Rate;
                      

                      }

                      Flummery:

                      This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                      L 1 Reply Last reply
                      0
                      • A Alan N

                        On which line does the compiler show the error? My guess is it goes wrong at

                        return Refurb_Rate;

                        as the method return type is Int32. If you were to change the declaration of Refurb_Rate from Double to Int32 then the problem would go away. I'm not sure what you are trying to do in the line

                        Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100);

                        It doesn't make much sense as the result of the expression on the RHS is already Int32 making the explicit conversion redundant. Alan.

                        J Offline
                        J Offline
                        Justiin1265
                        wrote on last edited by
                        #11

                        It shows the error at Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); I'm trying to convert the doulbe back to an integer, so I figured use the conversion syntax to return to an integer.

                        A 1 Reply Last reply
                        0
                        • J Justiin1265

                          Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          Justiin1265 wrote:

                          Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast.

                          this does not make sense, as all variables are declared integers, therefore the quotient will be integer too, and the Convert method does nothing for you. Furthermore, the result will not be what you expect: for totalRefurb=2, totalUnits=10, the formula looks like 2 / 10 * 100, unfortunately in an integer world 2 / 10 is zero. What you need to do is one of the following: - multiply first, i.e. 100 * totalRefurb / totalUnits - use real numbers (i.e. data types such as float or double), this will avoid overflow (when your integers near the max value), and perform rounding. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          modified on Monday, April 11, 2011 3:25 PM

                          A D 2 Replies Last reply
                          0
                          • A AspDotNetDev

                            I have a program for work that uses a formula to calculate the refurb on a unit (parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that, but still get the error "Cannot implicitly convert type double to int. An explicit conversion exists (are you missing a cast?)" What am I doing wrong? Here's some of my code to look at:

                            private int GetRefurbRate()
                            {
                            string sql = "";
                            double Refurb_Rate;
                            int totalRefurb = 0;
                            int totalUnits = 0;
                            string error_msg = "";

                            sql = "SELECT COUNT(rp.repair\_ord) " +
                                "FROM " + schema + ".repair\_part rp " +
                                "WHERE rp.repair\_ord = '" + repair\_ord + "' ";
                            while (true)
                            {
                                if (!myDb.RunSql(sql, true))
                                {
                                    error\_msg = "DBError for getting Refurb Rate";
                                    break;
                                }
                                if (myDb.dbRdr.HasRows)
                                {
                                    if (myDb.dbRdr.Read())
                                    {
                                        try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception.
                                        {
                                            Refurb\_Rate = Convert.ToInt32( totalRefurb / totalUnits \* 100); //This is where I try to perform the cast.
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e);
                                        }
                                    }
                            
                                    //int Refurb\_Rate = Convert.ToInt32(Refurb\_Rate);
                                }
                            
                                break;
                            }
                            myDb.dbRdr.Close();
                            
                            if (error\_msg != String.Empty)
                            {
                                MessageBox.Show(error\_msg, "Get Refurb Rate",
                                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            }
                            
                            return Refurb\_Rate;
                            

                            }

                            Flummery:

                            This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            Congrats on the PRE tags, however the percentage formula isn't quite OK... see my other msg. :)

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Justiin1265 wrote:

                              Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast.

                              this does not make sense, as all variables are declared integers, therefore the quotient will be integer too, and the Convert method does nothing for you. Furthermore, the result will not be what you expect: for totalRefurb=2, totalUnits=10, the formula looks like 2 / 10 * 100, unfortunately in an integer world 2 / 10 is zero. What you need to do is one of the following: - multiply first, i.e. 100 * totalRefurb / totalUnits - use real numbers (i.e. data types such as float or double), this will avoid overflow (when your integers near the max value), and perform rounding. :)

                              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                              modified on Monday, April 11, 2011 3:25 PM

                              A Offline
                              A Offline
                              AspDotNetDev
                              wrote on last edited by
                              #14

                              I deleted my crap responses. Yours is more... what is the word? Oh yes, "correct". :)

                              Flummery:

                              This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                              L 1 Reply Last reply
                              0
                              • A AspDotNetDev

                                I deleted my crap responses. Yours is more... what is the word? Oh yes, "correct". :)

                                Flummery:

                                This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                "More correct"? A purist might object, some characteristics really are binary. :-D I'm not saying it is correct now (I still don't get what the OP is after), I do think one more problem has been addressed though.

                                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                1 Reply Last reply
                                0
                                • J Justiin1265

                                  It shows the error at Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); I'm trying to convert the doulbe back to an integer, so I figured use the conversion syntax to return to an integer.

                                  A Offline
                                  A Offline
                                  AspDotNetDev
                                  wrote on last edited by
                                  #16

                                  Refurb_Rate is a double, so when you return it later on, it is still holding a double (even though that double may happen to be integral). Since your function is of return type "int", you need to ensure you return data type "int". Luc addresses some other issues with your code below.

                                  Flummery:

                                  This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.

                                  1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    Justiin1265 wrote:

                                    Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast.

                                    this does not make sense, as all variables are declared integers, therefore the quotient will be integer too, and the Convert method does nothing for you. Furthermore, the result will not be what you expect: for totalRefurb=2, totalUnits=10, the formula looks like 2 / 10 * 100, unfortunately in an integer world 2 / 10 is zero. What you need to do is one of the following: - multiply first, i.e. 100 * totalRefurb / totalUnits - use real numbers (i.e. data types such as float or double), this will avoid overflow (when your integers near the max value), and perform rounding. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                    modified on Monday, April 11, 2011 3:25 PM

                                    D Offline
                                    D Offline
                                    David1987
                                    wrote on last edited by
                                    #17

                                    Hey now, neither float nor double can represent all reals. They can only represent (some) rationals (and -0, NaN, +/-Infinity).

                                    L 1 Reply Last reply
                                    0
                                    • D David1987

                                      Hey now, neither float nor double can represent all reals. They can only represent (some) rationals (and -0, NaN, +/-Infinity).

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #18

                                      David1987 wrote:

                                      neither float nor double can represent all reals

                                      I agree (and I'll add nothing can represent just any real number's value); I also didn't say such thing. :)

                                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                      D 1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        David1987 wrote:

                                        neither float nor double can represent all reals

                                        I agree (and I'll add nothing can represent just any real number's value); I also didn't say such thing. :)

                                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                        D Offline
                                        D Offline
                                        David1987
                                        wrote on last edited by
                                        #19

                                        No.. but you said "real numbers" and "float" in the same sentence, it might give people the wrong idea. I've known plenty of people who were told by a teacher that doubles are "real numbers" (which is almost true if it's put like that - except for NaN and infinities and -0) and they were all confused and surprised when their calculations gave the "wrong" results because they expected all reals to be representable by doubles.

                                        L 1 Reply Last reply
                                        0
                                        • D David1987

                                          No.. but you said "real numbers" and "float" in the same sentence, it might give people the wrong idea. I've known plenty of people who were told by a teacher that doubles are "real numbers" (which is almost true if it's put like that - except for NaN and infinities and -0) and they were all confused and surprised when their calculations gave the "wrong" results because they expected all reals to be representable by doubles.

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          Yes, it would have been better to put float and real in code-style. I'll fix that. I use "real numbers" for non-integer numbers (or better yet, numbers not restricted to integer values), and not for "exact numbers". Most programmers do it that way; it may originate in the old days of Fortran, where they had type REAL (there wasn't a FLOAT time then). And calculus (as well as programming) courses should teach people about number representations, errors, accumulation and propagation of errors, etc. Most programmers understand the square root of two isn't exact inside a computer, yet many have a hard time understanding why one third times three isn't always one. :)

                                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                          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