Copyright 2005-2007 Curt Hill Casts What are they? Why do we need them?

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Integer Arithmetic. Operator Priority Real Number Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
ISBN Lecture 07 Expressions and Assignment Statements.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
Chapter 2 part #4 Operator
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Arithmetic Expressions
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Mixing integer and floating point numbers in an arithmetic operation.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © – Curt Hill Types What they do.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions and Assignment Statements
Side Effect Operators Changing the value of variables
More important details More fun Part 3
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
What are they? Why do we need them?
Expressions and Assignment Statements
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Expressions and Assignment Statements
College of Computer Science and Engineering
Arithmetic Expressions & Data Conversions
Chapter-3 Operators.
What are they? Why do we need them?
Expressions and Assignment
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 7 Expressions and Assignment Statements.
OPERATORS in C Programming
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Presentation transcript:

Copyright Curt Hill Casts What are they? Why do we need them?

Copyright Curt Hill Mixed Mode Arithmetic Multiple types in one arithmetic expression –3.5 * 2 –A real and integer constant Most programming languages allow some form of mixed mode C and C++ are quite liberal Java somewhat more restrictive

Copyright Curt Hill Better Example Suppose: int a=2, b; double d; cin >> b >> d; d = d*b – a; a = b*d – a; What is the difference? One loses information and the other does not!

Copyright Curt Hill Conversions C++ will perform certain conversions automatically Converting an integer value to a double value is automatic –Doubles are stronger –This conversion does not lose anything The conversion from double to int will also be done automatically –Even though information is lost

Copyright Curt Hill When is the conversion done? Consider the following: int a=2, b=3; double d = 3; d = a/b; What do we get? From an algebra view: 2/3 should be.6666… Actually we get zero

Copyright Curt Hill Why zero? C++ only looks at the immediate pair of operands and refuses to look ahead So it first does a/b, which is 2/3 Since both are integers, we do integer division This gives a zero

Copyright Curt Hill Observations C++ is very short sighted Even though the resulting expression of: d = a/b*d; is double, it does no conversion until needed It avoids some thornier problems by doing so

Copyright Curt Hill So what is the fix? How do we get 2/3 instead of zero? A cast –A cast is a conversion of type from one type to another The variable type is not changed! We extract the value and change the type of that value

Styles of Casts The compiler will do a cast automatically for mixed mode arithmetic –For operators convert weaker to stronger –For assignments convert to receiving type C style cast C++ style cast Copyright Curt Hill

The C Cast The form of a cast is the type in parenthesis This is a unary operator, it takes the operand on its right and converts the value into the appropriate type: (double)a/b It has very high precedence so it occurs before the division

Copyright Curt Hill The C++ Cast C++ introduced a cast that looks like a function call or constructor double(a)/b Use the type name like a function name Precedence is not an issue This can only be used for types with a single name –Use (long double)a/b for multiple names

Copyright Curt Hill The 2/3 problem solved We have two ways to accomplish what we wanted: d = double(a)/b; d = (double)a/b; d = a/double(b); These work, but these do not: d = (double)(a/b); d = double(a/b); –The damage is already done

Copyright Curt Hill More on casts The above example helped us to strengthen a weak type (int) into a stronger type (double) We can also go the other way: int a=2, b; cin >> b; double d cin>>d; a = b*d – a; // Truncation a = int (b*d-a); // Legal

Copyright Curt Hill Rounding How is a double converted into an int? Truncation! –(int) is zero! How do we round? Simple: add 0.5 prior to the cast (int)( ) is (int)(1.4999) is 1