CSC/ECE 517 Fall 2011/ch3 3h rr: Difference between revisions
Line 3: | Line 3: | ||
== Introduction == | == Introduction == | ||
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects. These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. The primitive types commonly included in most programming languages are: | Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects. These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. The primitive types commonly included in most programming languages are: | ||
* Boolean | * Boolean | ||
* Character | * Character |
Revision as of 17:18, 6 October 2011
3h. Primitive objects. At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby. Other languages, like Java, have made similar distinctions. By contrast, languages such as C# and Eiffel try to hide these implementation differences from users. Answer two questions: (1) How have different o-o languages implemented primitive objects? E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc. (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?
Introduction
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[1]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects. These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. The primitive types commonly included in most programming languages are:
- Boolean
- Character
- Integer
- Floating-point number
- Fixed-point number
- Reference ??
Boolean
A Boolean is a primitive data type used to store one of two logical types: true or false. Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types. Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type. In many languages, booleans can be implicitly converted to and from integer types.
Character
A character is a data type that represents an element of a written language, such as a letter, number, or symbol. A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed. Characters are commonly stored as integers, and encoded using a character map.
Integer
An integer is a data type that represents one element of a finite subset of mathematical integers. Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers). The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed). Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values. The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer. For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255. More generally, an n-bit unsigned integer can store from 0 to (2^n)-1. For signed integers, modern computers use the Two’s Complement encoding scheme. This allows for a range of −2^(n−1) through 2^(n−1)−1. For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.
Floating-Point Number
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision. In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.
Fixed-Point number
A fixed-point number is a data type used to represent real numbers. Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark. In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical. Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms.
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss. Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow. Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.
Primitive Objects in Ruby
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some inbuilt classes supported in Ruby. However, we feel that only some of them have corresponding representation in other languages. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:
- Bignum
- Fixnum
- Float
- Integer
- String
Each of these types also has support for certain methods defined for the specific objects.
Eg.