STEMINATE
  • Home
  • STEMinate Science Fair
  • About
    • Initiative and Advisory Board
    • Regional Directors and Chapters
  • Join
    • Application
  • Flyers!
  • Summer Camp
    • Videos of 2020 Summer Camp!
  • Journal
  • Lesson Plans
    • Environmental Science & Climate
    • Biology
    • Mathematics
    • Astronomy
  • Coding Tutorials
    • Python Course: Code Circle >
      • Python Course Week 1 >
        • Python Course Week 1 (Part 1): Introduction
        • Python Course Week 1 (Part 2): Repl.it Setup
      • Python Course Week 2: Numbers (Ints and Doubles) >
        • Python Course Week 2: Variables
        • Python Course Week 2: Integers
        • Python Course Week 2: Integer Operations
        • Python Course Week 2: Doubles
        • Python Course Week 2: Summary
      • Python Course Week 3: Strings >
        • Python Course Week 3: Printing, Assigning, & Concatenating
    • Python Syntax Course: Code Academy >
      • 1. "Hello World"
      • 2. Print Statements
      • 3. Strings
  • Science Kits
    • Geode in an Egg
    • Rock Crystallization
  • COVID-19 Tracker
  • Fundraisers
    • Heart to Heart International
    • FeedMore
  • STEM Programs
    • High School Students
    • Middle School Students
  • Contact
  • Photos

Doubles

What's the difference between Doubles and Ints?
Integers are positive and negative integers. For example, 2, 4, and -5 are integers.
Double precision floating numbers, more commonly known as doubles, are decimal numbers. For example, 1.333, -2.5, and 1.5 are doubles.

2 is an int, 2.0 is a double. For these numbers, it depends on how you write the number. 
In python, you don’t need to write 2.0. If you have a variable that is currently an integer (ex: a=3) and you add a double, it will automatically change to a double.
4.5 is not an int, it is a double.
Why are doubles important? Division.
Remember: when you coding, division ➗ is written as a forward slash /
Note the differences between these equations and what the results will be.
  • 10/4 = 2
  • 10.0/4.0 = 2.5
  • 10.0/4 = 2.5
  • 10/4.0 = 2.5
​If the two numbers that you use when dividing are BOTH INTS, the answers will always be an int. Think of it like this: the math is being done, except you cut off the non integer part. Or, the answer is the number of integer multiples of the divisor that go into the dividend.
However, if you have one or more doubles in the equation, the result is a double. This is like normal division.
Modulus %
Modulus is just like normal division, but the result is the REMAINDER.
  • 12%4 = 0
  • 10%3 = 1
  • 5%3 = 2
Picture
Picture
The arguments that you use (the numbers in the equation) can also be doubles. For example, 3.14%0.7 = 0.34 → 3.14 = 0.7(4) + 0.34.
The modulus operator (%)’s result is either the same sign as the divisor or 0. The result is also always smaller than the divisor. If you’re dealing with negative numbers, make sure that the ABSOLUTE VALUE of the result is smaller than the absolute value of the divisor.
When Would You Use a Double Instead of an Int? 
  • When you are dealing with decimals or fractions
  • When you have bigger numbers (not always necessary)
  • When you are using a lot of different number operations 
    • ex: +, -, / %, *
    • Sometimes the results can lead to decimal numbers so you can use a double in case that happens
  • If you want a higher degree of precision
Transferring
In the case about the dividing above, the result you get from division is only a double when one or more of the numbers in the function is a double. Here is the example again:
  • 10/4 = 2
  • 10.0/4.0 = 2.5
  • 10.0/4 = 2.5
  • 10/4.0 = 2.5​

The same thing applies to multiplication, so: 
  • int * int = int
  • double * int = double
  • int * double = double
  • double * double = double
  • The above example represented with numbers:
  • 2 * 4 = 8

If you divide an int by an int, the answer will always be an int, even if the int's don't evenly divide. To the programmer, it will look like the math is done and then the part of the number after the decimal is cut off. ​Or, the answer is the number of integer multiples of the divisor that go into the dividend.
​Example: 10/4 = 2
​If there are more than one doubles in the division operation, the resulting number will also be a double. This operation with probably what you think of as regular division where remainders are accounted for as the numbers after the decimal point.
Note: the rule about doubles for division also applies to multiplication, addition, and subtraction. In all of these cases, you only need one double in the operation for the resulting number to also be a double.
Picture
Picture
  • 2.0 * 4 = 8.0
  • 2 * 4.0 = 8.0
Picture
Picture
  • 2.0 * 4.0 = 8.0
If you want to transfer an int (say, 15) to a double, but you don’t want to change the actual value of the number:
  • 1.0 * 15 = 15.0
  • It’s like the identity property: if you multiply it by one, the value of the number doesn’t change, but multiplying it by a double makes the number turn into a double.​
Picture
There is another way to transfer an int to a double:
  • 15/1.0 = 15.0
  • The same concepts apply to this as they did to the multiplication method of transferring: dividing by 1 doesn’t change the number. However, dividing by an double changes the number to a double.
Usually, the most common method of transferring is by multiplying 1.0 to the number that you want to transfer.
Proudly powered by Weebly
  • Home
  • STEMinate Science Fair
  • About
    • Initiative and Advisory Board
    • Regional Directors and Chapters
  • Join
    • Application
  • Flyers!
  • Summer Camp
    • Videos of 2020 Summer Camp!
  • Journal
  • Lesson Plans
    • Environmental Science & Climate
    • Biology
    • Mathematics
    • Astronomy
  • Coding Tutorials
    • Python Course: Code Circle >
      • Python Course Week 1 >
        • Python Course Week 1 (Part 1): Introduction
        • Python Course Week 1 (Part 2): Repl.it Setup
      • Python Course Week 2: Numbers (Ints and Doubles) >
        • Python Course Week 2: Variables
        • Python Course Week 2: Integers
        • Python Course Week 2: Integer Operations
        • Python Course Week 2: Doubles
        • Python Course Week 2: Summary
      • Python Course Week 3: Strings >
        • Python Course Week 3: Printing, Assigning, & Concatenating
    • Python Syntax Course: Code Academy >
      • 1. "Hello World"
      • 2. Print Statements
      • 3. Strings
  • Science Kits
    • Geode in an Egg
    • Rock Crystallization
  • COVID-19 Tracker
  • Fundraisers
    • Heart to Heart International
    • FeedMore
  • STEM Programs
    • High School Students
    • Middle School Students
  • Contact
  • Photos