5.3.2. Numeric Types and Expressions (2024)

HOMEUP
Top/C++/Types and Experessions/Numbers

Numeric types

Numeric types are similar to those in Java, though less carefully defined. A particular operating system defines a word size: a 64-bit operating system uses 64 bits per word, and a 32-bit operating system uses 32 bits per word. Machine xlogin.cs.ecu.edu is a 64-bit machine.

An executable program (or more succinctly an executable) is a machine-language file that can be run directly on the computer. Some 64-bit operating systems can run programs compiled for 32-bit computers. So we will talk about 64-bit executables and 32-bit executables.

long

Values of type long are integers that are 64 bits long in a 64-bit executable and 32 bits long in a 32-bit executable. Use them for integers that might be fairly large. The largest integer that can be represented in 32 bits is roughly 2 billion, and the largest integer that can be represented in 64 bits is roughly 8 billion billion.


int

Values of type int are typically 32 bit integers.


short

Values of type short are typically 16-bit integers. The largest integer that you can store in a 16-bit integer is roughly 32,000.


unsigned int, unsigned long, unsigned short

Normally, integers have a sign bit that is used to tell whether the integer is positive or negative. An unsigned integer is always treated as nonnegative, and does not devote one of the bits to the sign. So you can store a larger integer in an unsigned variable. For example, the largest unsigned 32-bit integer is roughly 4 billion.


double

A value of type double is an approximation to a real number that typically uses 64 bits. That gives you about 15 digits of precision. Numbers are stored in base 2, not base 10, and you need to watch out for roundoff error. For example, 0.1 (one-tenth) cannot be represented exactly in base 2 with a finite number of digits, so it is rounded, similarly to the way 1/3 (0.33333...) is rounded in based 10.


float

A value of type float is an approximation to a real number that typically uses 32 bits. That gives you about 7 digits of precision. Because computers can perform many millions of operations in a single computation, roundoff error becomes a serious problem, and most programmers prefer to use type double for most real numbers.



Numeric constants

234

Write an integer constant in the usual way. Do not write commas in a number.


123456789L

Normally, an integer constant is assumed to have type int. Add L after the number to indicate that it has type long. Other suffixes are U (unsigned int) and UL (unsigned long).


234.5

Write real numbers in the usual way. Note that 3 has type int but 3.0 has type double.


234.5E12

An E indicates a power of 10. Constant 234.5E12 is 234.5×1012 and 1.0E−30 is 1.0×10−30.


0xA2

Precede an integer constant by 0x to indicate that it is written in base 16 (hexadecimal). The digits are 0,1, ...,9,A,B,C,D,E,F, where A stands for 10, B stands for 11, etc. You can use either upper case A-F or lower case a-f as digits.


011

An integer constant that starts with 0 is assumed to be written in base 8 (octal). For example, 011 is the same as 9.



Coercion and conversion

Numbers are converted automatically from one type to another where necessary. For example, 2 is converted to 2.0 if it occurs where a value of type double is needed. A value of type long is converted to type int automatically by selecting only the rightmost 32 bits. We will see examples of conversions below.

Converting an integer to a real number is usually harmless. Converting a real number to an integer throws away everything after the decimal point. So if 2.7 occurs in a place where an integer is required, it is converted to 2. Watch out for that.

You can request conversions explicitly. If T is a numeric type, then write (T), with the parentheses, in front of an expression to convert the value of that expression to type T. For example, expression (int)(x + y) is the value of x + y converted to type int.


Numeric operators

x + y

The sum of x and y.


xy

The difference, xy.


x

The negation of x.


x * y

The product x times y.


x / y

The quotient x divided by y. Important: If x and y are both integers then x/y is also an integer. Anything after the decimal point is ignored. So 7/4 has value 1, not 1.75.


x % y

The remainder when you divide integer x by integer y. For example, when you divide 14 by 3 you get a quotient of 4 and a remainder of 2. So 14 % 3 = 2. You can only use the % operator on integers.



Precedence and associativity

Operator * has higher precedence than operator +. For example, expression 2 + 5 * 2 has value 12, since the multiplication is done first.

Operators *, / and % have the same precedence, and operators + and − have the same precedence. All of them are performed from left to right, except where precedence rules and parentheses force otherwise. For example, 10 − 2 − 3 has value 5. Notice that 16/2*2 has value 16 since * and / have the same precedence. The division 16/2 is done first, and then the result of that division is multiplied by 2. If you want 16/(2*2), be sure to use parentheses.


Some numeric functions

If you #include <cmath> then you can use the following functions. In all cases, x and y have type double, n has type int and L has type long. Variables u and v can be of any numeric type.

sqrt(x)

Approximately the square root of x.


pow(x, y)

Approximately xy, where x and y are real numbers.


abs(n)

The absolute value of int n.


labs(L)

The absolute value of long integer L.


fabs(x)

The absolute value of real number x.


If you #include <algorithm> then you can use the following functions.

max(u, v)

The larger of u and v. For example, max(3,7) = 7. This works for any numeric type.


min(u, v)

The smaller of u and v. For example, min(3,7) = 3. This works for any numeric type.



Integer overflow

Each size of integer (given by a number of bits used to represent it) has a largest allowed positive value. If you add 1 to that number, you will not get an error. Instead, the result will be a negative number. For example, the largest value that can be stored in a variable of type short is 32,767. Statement

 short n = 32767; short m = n + 1;
makes m = −32768, the smallest negative number that can be stored in a variable of type short.

Exercises

  1. Why do most programmers prefer to use type double instead of type float for most uses? Answer

  2. What is the value of expression 9 − 6 + 3? Answer

  3. What is the value of expression 3/5? Answer

  4. What is the value of expression 25 % 4? Answer

  5. What is the value of expression 0 % 5 Answer

  6. What is the value of expression sqrt(25.0) Answer

  7. What is the value of expression max(3,8) Answer

  8. Is [3 + 2] * 8 an allowed C++ expression? Answer

HOMEUP
Top/C++/Types and Experessions/Numbers
5.3.2. Numeric Types and Expressions (2024)

FAQs

How to write in numeric form? ›

Hint: To write a number in word form or from word to number see the place value of the digits of the number and write accordingly. One's place is written as 1, tens place as 10, hundreds place as 100 and so on. So, if we have a number as 25, then we have 5 in ones' place and 2 (as 20) in ten's place.

What are the numeric data types and operations? ›

A Numeric Data Type is defined as a type of data that represents continuous numerical values which can be expressed with numbers and have an infinite number of values between digits. It allows for mathematical operations like addition, subtraction, and logical comparisons.

How do you write a numeric expression? ›

There can be one or more operators in a numerical expression. For instance, 2 + 17 is a numerical expression with one operator that is addition. 11 + 43 x ( -4) is a numerical expression with three operators addition followed by multiplication and subtraction.

What is a numeric example? ›

Numeric numbers, also known as “numerals” or “digits”, are the symbols we use to represent numbers in computing and mathematics. They range from 0 to 9 and can be combined to create larger values (i.e 123 is composed of three numeric components: 1, 2 and 3).

What is an example of a numeric data type? ›

Numeric Data Types

Examples of numeric data types are examination marks, height, weight, the number of students in a class, share values, price of goods, monthly bills, fees and others. In Visual Basic, numeric data are divided into 7 types, depending on the range of values they can store.

What are the 2 types of numerical data type? ›

The two major types of numerical data are discrete and continuous. Discrete data is a type of numerical data which specific or fixed data values. Continuous data is data which lies within a given range of values. Operations can be performed on numerical data.

What are the three basic numeric types? ›

Numeric Types — int , float , complex. There are three distinct numeric types: integers, floating-point numbers, and complex numbers.

What is an example of a numerical form? ›

Answer: numerical form is the number of example:1,2,3,4,5,6,7,8,9,...

What is numerical form in writing? ›

A simple rule for using numbers in writing is that small numbers ranging from one to ten (or one to nine, depending on the style guide) should generally be spelled out. Larger numbers (i.e., above ten) are written as numerals.

Top Articles
BlockChain Objective
Steps for Changing Azure Log Analytics Retention Period
What Did Bimbo Airhead Reply When Asked
UPS Paketshop: Filialen & Standorte
Amc Near My Location
Craigslist Vans
Kaydengodly
Bucks County Job Requisitions
Pitt Authorized User
Premier Boating Center Conroe
Sport Clip Hours
Diablo 3 Metascore
2016 Ford Fusion Belt Diagram
Mineral Wells Independent School District
Colorado mayor, police respond to Trump's claims that Venezuelan gang is 'taking over'
What Happened To Anna Citron Lansky
Locate At&T Store Near Me
Niche Crime Rate
Wgu Academy Phone Number
Project, Time & Expense Tracking Software for Business
Www.publicsurplus.com Motor Pool
Craigslist Battle Ground Washington
Valic Eremit
Hctc Speed Test
Arrest Gif
Jesus Revolution Showtimes Near Regal Stonecrest
Speedstepper
Free T33N Leaks
2004 Honda Odyssey Firing Order
Frank Vascellaro
Korg Forums :: View topic
Robot or human?
Does Iherb Accept Ebt
Western Gold Gateway
Vanessa West Tripod Jeffrey Dahmer
Bimmerpost version for Porsche forum?
How To Get Soul Reaper Knife In Critical Legends
Wsbtv Fish And Game Report
NHL training camps open with Swayman's status with the Bruins among the many questions
Dr Adj Redist Cadv Prin Amex Charge
Anhedönia Last Name Origin
Gasoline Prices At Sam's Club
Promo Code Blackout Bingo 2023
Shell Gas Stations Prices
Rs3 Nature Spirit Quick Guide
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
Oklahoma City Farm & Garden Craigslist
Matt Brickman Wikipedia
Suzanne Olsen Swift River
Affidea ExpressCare - Affidea Ireland
Ravenna Greataxe
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6261

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.