A Decimal Floating-Point Speciftcation

Total Page:16

File Type:pdf, Size:1020Kb

A Decimal Floating-Point Speciftcation A Decimal Floating-point Specification Michael F. Cowlishaw, Eric M. Schwarz, Ronald M. Smith, Charles F. Webb IBM UK IBM Server Division P.O. Box 31, Birmingham Rd. 2455 South Rd., MS:P310 Warwick CV34 5JL. UK Poughkeepsie, NY 12601 USA [email protected] [email protected] Abstract ing is required. In the fixed point formats, rounding must be explicitly applied in software rather than be- Even though decimal arithmetic is pervasive in fi- ing provided by the hardware. To address these and nancial and commercial transactions, computers are other limitations, we propose implementing a decimal stdl implementing almost all arithmetic calculations floating-point format. But what should this format be? using binary arithmetic. As chip real estate becomes This paper discusses the issues of defining a decimal cheaper it is becoming likely that more computer man- floating-point format. ufacturers will provide processors with decimal arith- First, we consider the goals of the specification. It metic engines. Programming languages and databases must be compliant with standards already in place. are expanding the decimal data types available whale One standard we consider is the ANSI X3.274-1996 there has been little change in the base hardware. As (Programming Language REXX) [l]. This standard a result, each language and application is defining a contains a definition of an integrated floating-point and different arithmetic and few have considered the efi- integer decimal arithmetic which avoids the need for ciency of hardware implementations when setting re- two distinct data types and representations. The other quirements. relevant standard is the ANSI/IEEE 854-1987 (Radix- In this paper, we propose a decimal format which Independent Floating-point Arithmetic) [a]. The IEEE meets the requirements of existing standards for deci- 754 standard [3] is a binary standard which also mal arithmetic and as eficient for hardware implemen- meets this radix-independent standard. The radix- tation. We propose this specification in the hope that independent standard gives us guidelines for formats designers will consider providing decimal arithmetic in and exponent ranges that need to be considered and future microprocessors and that future decimal software specifically describes requirements for base 10 floating specifications will consider hardware efficiencies. point. A floating-point format must also permit efficient implementation. One of the difficulties with decimal 1. Introduction arithmetic is that it is less efficient than binary arith- metic. Representations take up more memory and exe- People habitually perform arithmetic in base 10. cution time of an operation is inherently longer. How- When calculations are moved to computers there is ever, if the data is already stored in memory in a dec- usually a loss in translating decimal fractions to a imal format and conversions between decimal and bi- binary representation. Common decimal values such nary formats are costly, then it can be more efficient as 0.1 can be represented exactly in decimal but can to carry out computations directly in decimal. Fur- only be approximated in a binary floating-point for- ther, if the chosen format requires very little hardware mat. Traditionally, financial transactions are therefore to support then it is likely that future microproces- performed in a scaled binary or decimal integer for- sors would implement it in hardware, just as most im- mat. Both of these formats are fixed point and so, plement binary floating-point arithmetic today. From even though they avoid fraction approximation, they a financial and business software standpoint, decimal are severely limited in the range of numbers they can floating-point hardware is as attractive as a graphics represent. Further, in many applications, such as the engine is attractive to a games developer. calculation of a monthly mortgage payment, round- Other considerations in developing a specification 147 0-769s-I 150-3/01 $10.00 0 2001 IEEE Language Platform Support Precision Comments c & c++ S/390 Fixed 31 C only AS1400 Fixed 31 C only Others Fixed 31-38 In various libraries COBOL All Fixed 31 32 digit Floating proposed C# All Float 28 Java All Floating infinite Using IBM BigDecimal class; java.math is fixed point os/400 CL AS/4000 Fixed 15 Scale <= 9 PL/I S/390 Fixed 15 The FLOAT Decimal data type is actually binary AS1400 Fixed 15 Others Fixed 31 PSM All Fixed 31 Implemented via translation to C Rexx All Floating infinite RPG AS1400 Fixed 30 Scale <= 9 Table 1. Software and Platforms Implementing Decimal Arithmetic are that it should be efficient for reading and storing The remainder of this paper discusses a specifica- decimal data in databases. Decimal data are usually tion for decimal floating-point data types which best stored in Binary Coded Decimal (BCD) notation. Typ- meets these goals and fits within appropriate lengths. ical decimal data are fixed point with up to 20 dig- Existing hardware architectures suggest that represen- its of precision, including up to 10 fractional digits. tations be 32, 64,80, or 128 bits in length, and these are There needs to be efficiency in reading the data out convenient for software, too. It is proposed that only of a database, operating on it, scaling and rounding 64 and 128 bit representations be defined at this time, it, and then storing the data back into the database forming the single and double formats as described in with a fixed point notation. These requirements favor the IEEE 854 standard. Some prior work by Johnstone a BCD format. and Petry [4] showed a 32 bit format but this length A further constraint on a specification is the need to does not meet most software requirements. Hull [5] allow for future expansion of the notation. An architec- suggests a variable precision format but this does not ture or format should be robust enough to grow with map easily to hardware implementations. Note that future changing needs. One possible mechanism could the 854 standard requires a single precision be defined be through the use of special values. There needs to be and recommends that there should be an extended pre- enough room in the format to support infinities, Not-a- cision. Double precision satisfies the requirements of Number (NaN) values, and both positive and negative single-extended precision. zeros. In the future there might be additional values, Software requirements are first discussed, followed such as subnormal numbers, considered too. by the overall decimal format, which is similar to that In summary, the goals of the specification are: described by Ris[G]. These are followed by discussion on the efficiency and requirements for each part of the 1. It should allow efficient hardware or software im- format, including the representation of the integer, ex- plementation of decimal floating-point ponent, and special values, the ordering of parts, and 2. It should support numbers to be used for the the length of the exponents. Finally, complete formats floating-point and integer decimal arithmetic de- will be described. fined in ANSI X3.274-1996 (Programming Lan- guage REXX) 2. Software Requirements 3. It should support numbers and values to be used for the floating-point arithmetic defined Table 1 shows a list of several common languages in ANSI/IEEE 854-1987 (Radix-Independent which define decimal arithmetic (sometimes using li- Floating-point Arithmetic) braries). Also listed is the support that is provided on 4. It should allow efficient use of existing data in a given platform. The IBM S/390 (2-Series) platform databases has 31 digit decimal integer arithmetic hardware and this is used by many of the languages including C and 5. It should allow for future expansion. COBOL. Java and REXX both provide floating-point 148 decimal representation with an unlimited number of the data in memory, which are typically in BCD for- digits. For other languages, the required support is ap- mat, or it could be a more compact format. If it is proximately 31 or 32 decimal digits. There are some more compact then a transformation step is required instances of 38 digit support in both languages and to move data from memory into the internal format. databases. Hence, the maximum requirement in prac- Other effects that are important are that overflows or tice appears to be 38 digits, although 31 or 32 digits rounding precision boundaries are more easily spotted meets the needs of almost all real applications. if the format allows easy identification of decimal digit boundaries. Similarly, carries can easily be detected 3. Decimal Arithmetic Specification and renormalization of the representation is made eas- ier if decimal digit boundaries are preserved. On the other hand, decimal adders are slightly slower than bi- There has already been work in defining a standard nary adders and more complex operations are corre- decimal floating-point arithmetic specification [7, 81. spondingly slower, too. Therefore, the integer format These documents define a set of operations and rules, must be picked carefully. but do not define a concrete representation of numbers. The three most interesting formats for the integer The present study discusses the choice of representa- are: 1) Binary format , 2) Binary Coded Decimal for- tion. mat (BCD, 4 bits per digit), 3) Compressed BCD for- These other decimal arithmetic specifications [7, 81, mat. however, detail many useful aspects such as the oper- Binary representation is the most compact form and ations, formats, and rounding modes that need to be allows very fast multiplications and divisions. In this supported to be useful for multiple languages and ap- format, additions and subtractions which require shift- plications. The core operations needed are Add, Sub- ing are slow, and rounding and normalization can be tract, Plus, Minus, Multiply, Divide, Power, Divide- difficult.
Recommended publications
  • Fujitsu SPARC64™ X+/X Software on Chip Overview for Developers]
    White paper [Fujitsu SPARC64™ X+/X Software on Chip Overview for Developers] White paper Fujitsu SPARC64™ X+/X Software on Chip Overview for Developers Page 1 of 13 www.fujitsu.com/sparc White paper [Fujitsu SPARC64™ X+/X Software on Chip Overview for Developers] Table of Contents Table of Contents 1 Software on Chip Innovative Technology 3 2 SPARC64™ X+/X SIMD Vector Processing 4 2.1 How Oracle Databases take advantage of SPARC64™ X+/X SIMD vector processing 4 2.2 How to use of SPARC64™ X+/X SIMD instructions in user applications 5 2.3 How to check if the system and operating system is capable of SIMD execution? 6 2.4 How to check if SPARC64™ X+/X SIMD instructions indeed have been generated upon compilation of a user source code? 6 2.5 Is SPARC64™ X+/X SIMD implementation compatible with Oracle’s SPARC SIMD? 6 3 SPARC64™ X+/X Decimal Floating Point Processing 8 3.1 How Oracle Databases take advantage of SPARC64™ X+/X Decimal Floating-Point 8 3.2 Decimal Floating-Point processing in user applications 8 4 SPARC64™ X+/X Extended Floating-Point Registers 9 4.1 How Oracle Databases take advantage of SPARC64™ X+/X Decimal Floating-Point 9 5 SPARC64™ X+/X On-Chip Cryptographic Processing Capabilities 10 5.1 How to use the On-Chip Cryptographic Processing Capabilities 10 5.2 How to use the On-Chip Cryptographic Processing Capabilities in user applications 10 6 Conclusions 12 Page 2 of 13 www.fujitsu.com/sparc White paper [Fujitsu SPARC64™ X+/X Software on Chip Overview for Developers] Software on Chip Innovative Technology 1 Software on Chip Innovative Technology Fujitsu brings together innovations in supercomputing, business computing, and mainframe computing in the Fujitsu M10 enterprise server family to help organizations meet their business challenges.
    [Show full text]
  • CWM) Specification
    Common Warehouse Metamodel (CWM) Specification Volume 1 Version 1.0 October 2001 Copyright © 1999, Dimension EDI Copyright © 1999, Genesis Development Corporation Copyright © 1999, Hyperion Solutions Corporation Copyright © 1999, International Business Machines Corporation Copyright © 1999, NCR Corporation Copyright © 2000, Object Management Group Copyright © 1999, Oracle Corporation Copyright © 1999, UBS AG Copyright © 1999, Unisys Corporation The companies listed above have granted to the Object Management Group, Inc. (OMG) a nonexclusive, royalty-free, paid up, worldwide license to copy and distribute this document and to modify this document and distribute copies of the mod- ified version. Each of the copyright holders listed above has agreed that no person shall be deemed to have infringed the copyright in the included material of any such copyright holder by reason of having used the specification set forth herein or having conformed any computer software to the specification. PATENT The attention of adopters is directed to the possibility that compliance with or adoption of OMG specifications may require use of an invention covered by patent rights. OMG shall not be responsible for identifying patents for which a license may be required by any OMG specification, or for conducting legal inquiries into the legal validity or scope of those patents that are brought to its attention. OMG specifications are prospective and advisory only. Prospective users are responsible for protecting themselves against liability for infringement of patents. NOTICE The information contained in this document is subject to change without notice. The material in this document details an Object Management Group specification in accordance with the license and notices set forth on this page.
    [Show full text]
  • Chapter 2 DIRECT METHODS—PART II
    Chapter 2 DIRECT METHODS—PART II If we use finite precision arithmetic, the results obtained by the use of direct methods are contaminated with roundoff error, which is not always negligible. 2.1 Finite Precision Computation 2.2 Residual vs. Error 2.3 Pivoting 2.4 Scaling 2.5 Iterative Improvement 2.1 Finite Precision Computation 2.1.1 Floating-point numbers decimal floating-point numbers The essence of floating-point numbers is best illustrated by an example, such as that of a 3-digit floating-point calculator which accepts numbers like the following: 123. 50.4 −0.62 −0.02 7.00 Any such number can be expressed as e ±d1.d2d3 × 10 where e ∈{0, 1, 2}. (2.1) Here 40 t := precision = 3, [L : U] := exponent range = [0 : 2]. The exponent range is rather limited. If the calculator display accommodates scientific notation, e g., 3.46 3 −1.56 −3 then we might use [L : U]=[−9 : 9]. Some numbers have multiple representations in form (2.1), e.g., 2.00 × 101 =0.20 × 102. Hence, there is a normalization: • choose smallest possible exponent, • choose + sign for zero, e.g., 0.52 × 102 → 5.20 × 101, 0.08 × 10−8 → 0.80 × 10−9, −0.00 × 100 → 0.00 × 10−9. −9 Nonzero numbers of form ±0.d2d3 × 10 are denormalized. But for large-scale scientific computation base 2 is preferred. binary floating-point numbers This is an important matter because numbers like 0.2 do not have finite representations in base 2: 0.2=(0.001100110011 ···)2.
    [Show full text]
  • Decimal Floating Point for Future Processors Hossam A
    1 Decimal Floating Point for future processors Hossam A. H. Fahmy Electronics and Communications Department, Cairo University, Egypt Email: [email protected] Tarek ElDeeb, Mahmoud Yousef Hassan, Yasmin Farouk, Ramy Raafat Eissa SilMinds, LLC Email: [email protected] F Abstract—Many new designs for Decimal Floating Point (DFP) hard- Simple decimal fractions such as 1=10 which might ware units have been proposed in the last few years. To date, only represent a tax amount or a sales discount yield an the IBM POWER6 and POWER7 processors include internal units for infinitely recurring number if converted to a binary decimal floating point processing. We have designed and tested several representation. Hence, a binary number system with a DFP units including an adder, multiplier, divider, square root, and fused- multiply-add compliant with the IEEE 754-2008 standard. This paper finite number of bits cannot accurately represent such presents the results of using our units as part of a vector co-processor fractions. When an approximated representation is used and the anticipated gains once the units are moved on chip with the in a series of computations, the final result may devi- main processor. ate from the correct result expected by a human and required by the law [3], [4]. One study [5] shows that in a large billing application such an error may be up to 1 WHY DECIMAL HARDWARE? $5 million per year. Ten is the natural number base or radix for humans Banking, billing, and other financial applications use resulting in a decimal number system while a binary decimal extensively.
    [Show full text]
  • Design of 32 Bit Floating Point Addition and Subtraction Units Based on IEEE 754 Standard Ajay Rathor, Lalit Bandil Department of Electronics & Communication Eng
    International Journal of Engineering Research & Technology (IJERT) ISSN: 2278-0181 Vol. 2 Issue 6, June - 2013 Design Of 32 Bit Floating Point Addition And Subtraction Units Based On IEEE 754 Standard Ajay Rathor, Lalit Bandil Department of Electronics & Communication Eng. Acropolis Institute of Technology and Research Indore, India Abstract Precision format .Single precision format, Floating point arithmetic implementation the most basic format of the ANSI/IEEE described various arithmetic operations like 754-1985 binary floating-point arithmetic addition, subtraction, multiplication, standard. Double precision and quad division. In science computation this circuit precision described more bit operation so at is useful. A general purpose arithmetic unit same time we perform 32 and 64 bit of require for all operations. In this paper operation of arithmetic unit. Floating point single precision floating point arithmetic includes single precision, double precision addition and subtraction is described. This and quad precision floating point format paper includes single precision floating representation and provide implementation point format representation and provides technique of various arithmetic calculation. implementation technique of addition and Normalization and alignment are useful for subtraction arithmetic calculations. Low operation and floating point number should precision custom format is useful for reduce be normalized before any calculation [3]. the associated circuit costs and increasing their speed. I consider the implementation ofIJERT IJERT2. Floating Point format Representation 32 bit addition and subtraction unit for floating point arithmetic. Floating point number has mainly three formats which are single precision, double Keywords: Addition, Subtraction, single precision and quad precision. precision, doubles precision, VERILOG Single Precision Format: Single-precision HDL floating-point format is a computer number 1.
    [Show full text]
  • Floating Point Representation (Unsigned) Fixed-Point Representation
    Floating point representation (Unsigned) Fixed-point representation The numbers are stored with a fixed number of bits for the integer part and a fixed number of bits for the fractional part. Suppose we have 8 bits to store a real number, where 5 bits store the integer part and 3 bits store the fractional part: 1 0 1 1 1.0 1 1 $ 2& 2% 2$ 2# 2" 2'# 2'$ 2'% Smallest number: Largest number: (Unsigned) Fixed-point representation Suppose we have 64 bits to store a real number, where 32 bits store the integer part and 32 bits store the fractional part: "# "% + /+ !"# … !%!#!&. (#(%(" … ("% % = * !+ 2 + * (+ 2 +,& +,# "# "& & /# % /"% = !"#× 2 +!"&× 2 + ⋯ + !&× 2 +(#× 2 +(%× 2 + ⋯ + ("%× 2 0 ∞ (Unsigned) Fixed-point representation Range: difference between the largest and smallest numbers possible. More bits for the integer part ⟶ increase range Precision: smallest possible difference between any two numbers More bits for the fractional part ⟶ increase precision "#"$"%. '$'#'( # OR "$"%. '$'#'(') # Wherever we put the binary point, there is a trade-off between the amount of range and precision. It can be hard to decide how much you need of each! Scientific Notation In scientific notation, a number can be expressed in the form ! = ± $ × 10( where $ is a coefficient in the range 1 ≤ $ < 10 and + is the exponent. 1165.7 = 0.0004728 = Floating-point numbers A floating-point number can represent numbers of different order of magnitude (very large and very small) with the same number of fixed bits. In general, in the binary system, a floating number can be expressed as ! = ± $ × 2' $ is the significand, normally a fractional value in the range [1.0,2.0) .
    [Show full text]
  • Floating Point Numbers and Arithmetic
    Overview Floating Point Numbers & • Floating Point Numbers Arithmetic • Motivation: Decimal Scientific Notation – Binary Scientific Notation • Floating Point Representation inside computer (binary) – Greater range, precision • Decimal to Floating Point conversion, and vice versa • Big Idea: Type is not associated with data • MIPS floating point instructions, registers CS 160 Ward 1 CS 160 Ward 2 Review of Numbers Other Numbers •What about other numbers? • Computers are made to deal with –Very large numbers? (seconds/century) numbers 9 3,155,760,00010 (3.1557610 x 10 ) • What can we represent in N bits? –Very small numbers? (atomic diameter) -8 – Unsigned integers: 0.0000000110 (1.010 x 10 ) 0to2N -1 –Rationals (repeating pattern) 2/3 (0.666666666. .) – Signed Integers (Two’s Complement) (N-1) (N-1) –Irrationals -2 to 2 -1 21/2 (1.414213562373. .) –Transcendentals e (2.718...), π (3.141...) •All represented in scientific notation CS 160 Ward 3 CS 160 Ward 4 Scientific Notation Review Scientific Notation for Binary Numbers mantissa exponent Mantissa exponent 23 -1 6.02 x 10 1.0two x 2 decimal point radix (base) “binary point” radix (base) •Computer arithmetic that supports it called •Normalized form: no leadings 0s floating point, because it represents (exactly one digit to left of decimal point) numbers where binary point is not fixed, as it is for integers •Alternatives to representing 1/1,000,000,000 –Declare such variable in C as float –Normalized: 1.0 x 10-9 –Not normalized: 0.1 x 10-8, 10.0 x 10-10 CS 160 Ward 5 CS 160 Ward 6 Floating
    [Show full text]
  • Floating Point Numbers Dr
    Numbers Representaion Floating point numbers Dr. Tassadaq Hussain Riphah International University Real Numbers • Two’s complement representation deal with signed integer values only. • Without modification, these formats are not useful in scientific or business applications that deal with real number values. • Floating-point representation solves this problem. Floating-Point Representation • If we are clever programmers, we can perform floating-point calculations using any integer format. • This is called floating-point emulation, because floating point values aren’t stored as such; we just create programs that make it seem as if floating- point values are being used. • Most of today’s computers are equipped with specialized hardware that performs floating-point arithmetic with no special programming required. —Not embedded processors! Floating-Point Representation • Floating-point numbers allow an arbitrary number of decimal places to the right of the decimal point. For example: 0.5 0.25 = 0.125 • They are often expressed in scientific notation. For example: 0.125 = 1.25 10-1 5,000,000 = 5.0 106 Floating-Point Representation • Computers use a form of scientific notation for floating-point representation • Numbers written in scientific notation have three components: Floating-Point Representation • Computer representation of a floating-point number consists of three fixed-size fields: • This is the standard arrangement of these fields. Note: Although “significand” and “mantissa” do not technically mean the same thing, many people use these terms interchangeably. We use the term “significand” to refer to the fractional part of a floating point number. Floating-Point Representation • The one-bit sign field is the sign of the stored value.
    [Show full text]
  • Floating Point Numbers
    Floating Point Numbers Yes, this is the moon; our own moon. Not the final frontier but the first out post there to be exploited by our greed of consumable minerals. Soon we the human race will be there blasting the mines and depriving the orb with its riches. Do we know how much is there to steal? Pop Quiz : What is the surface area of moon? 2 Answer : The surface area of a sphere is: 4 * π * R Radius of moon is about 1738.2 KM; plugging the values: 4 * 3.14159265 * 1738.2 * 1738.2 = 37967268 .598162344 KM 2. That would be 37.9 million square kilometers. Two of our biggest states Texas and California are 1.7 and 0.7 million square kilometers respectively. Surface if the Moon would be about 2/3 of the area of North America or about the size of Russia, that is close to the 38 million Sq Km Now you, all mainframe assembly language tool developers i.e. old time MF programmers try doing this calculation in S/390 Assembly. Give yourself few minutes. Address Object Code S/390 Assembly Reg./ Memory after execution 000036 B375 0010 LZDR R1 FPR1 00000000_00000000 00003A ED10 C08C 0024 LDE R1,FOUR FPR1 41400000_00000000 000040 7C10 C090 MDE R1,PIE FPR1 41C90FDC_00000000 000044 7C10 C094 MDE R1,RADIUS FPR1 445552DD_F73CD400 000048 7C10 C094 MDE R1,RADIUS FPR1 47243559_FE390700 00004C B3C9 0011 CGDR R1,0,R1 GR1 0243559F 000050 5010 C098 ST R1,FIXED 000054 4E10 C09C CVD R1,DECIMAL 00000003 7967263C 000088 45B27570 FLOAT DC X'45B27570' 00008C 41400000 FOUR DC E'4' 000090 413243F7 PIE DC E'3.14159265E+0' 000094 436CA333 RADIUS DC E'1.7382E+3' 000098 00000000 FIXED DC F'0' 00009C 0000000000000000 DECIMAL DC 2F'0' This is one way of solving the problem mentioned on previous slide and, of course, we all know that there are several different ways to solve any programming problem and your way is always better than mine.
    [Show full text]
  • Data Types and Variables
    Color profile: Generic CMYK printer profile Composite Default screen Complete Reference / Visual Basic 2005: The Complete Reference / Petrusha / 226033-5 / Chapter 2 2 Data Types and Variables his chapter will begin by examining the intrinsic data types supported by Visual Basic and relating them to their corresponding types available in the .NET Framework’s Common TType System. It will then examine the ways in which variables are declared in Visual Basic and discuss variable scope, visibility, and lifetime. The chapter will conclude with a discussion of boxing and unboxing (that is, of converting between value types and reference types). Visual Basic Data Types At the center of any development or runtime environment, including Visual Basic and the .NET Common Language Runtime (CLR), is a type system. An individual type consists of the following two items: • A set of values. • A set of rules to convert every value not in the type into a value in the type. (For these rules, see Appendix F.) Of course, every value of another type cannot always be converted to a value of the type; one of the more common rules in this case is to throw an InvalidCastException, indicating that conversion is not possible. Scalar or primitive types are types that contain a single value. Visual Basic 2005 supports two basic kinds of scalar or primitive data types: structured data types and reference data types. All data types are inherited from either of two basic types in the .NET Framework Class Library. Reference types are derived from System.Object. Structured data types are derived from the System.ValueType class, which in turn is derived from the System.Object class.
    [Show full text]
  • Using Data in Programs T
    Using Data in Programs HUS FAR IN THE VISUAL BASIC PROJECTS, we have used objects, Tmodified the properties and exercised some methods. We have used events to provide action in the projects. However, we have not used data except for text strings, and have not made any mathematical calcu- lations. That is about to change. We are now going to enter numeric data in the project, and perform some calculations. This will give us much more capability to do something useful than we have done so far. Variables and Constants Broadly speaking, data can be divided into two basic categories: (1) data that is changeable (or will likely change), and (2) data that will not change. In computer-eze, data that either can change or will likely change is called a variable, while data that does not change is called a constant. Most of the data you will deal with will be variables, but constants are also around us. Visual Basic treats them the same in some respects but has important differences. 65 Constants are values that have been given a specific definition, and nothing you can do will change it. For example, there are 7 days in a week, 24 hours in a day, 60 seconds in a minute, 12 months in a year, 12 inches in a foot, 5,280 feet in a mile, and so forth. Nobody can change them, and they are “constant” in their definition. A variable is data that will or could change. Examples are the number of work hours in a work week, rate of pay, scores on exams, price of a movie ticket, number of people attending an event, and so forth.
    [Show full text]
  • CPS 506 Comparative Programming Languages Type Systems Type
    CPS 506 Comparative Programming Languages Type Systems, Semantics and Data TpsTypes Type Systems • A comple te ly dfidefine d language: Defined syntax, semantics and type system • Type: A set of values and operations – int • Values=Z • Operations={+, -, *, /, mod} – Boolean • Values={true, false} • Operations={AND, OR, NOT, XOR} 2 Type Systems • Type SSstystem – A system of types and their associated variables and objects in a program – To formalize the definition of data types and their usage in a programming language – A bridge between syntax and semantics • Type checked in compile time: a part of syntax analysis • Type checked in run time: a part of semantics 3 Type Systems (con’t ) • SillStatically TdTyped: each variiblable is associated with a singgyple type during its life in run time. –Could be explicit or implicit declaration –Example: C and Java, Perl –Type rules are defined on abstract syntax (Static Semantics) 4 Type Systems (con’t ) • DillDynamically TdTyped: a varibliable type can be changed in run time – Example: LISP, JavaScript, PHP Java Script example: Lis t = [10. 2 , 3. 5] … List = 47 – Less reliable, difficult to debug – More flexible – Fast compilation – Slow execution (Type checking in run-time) 5 Type Systems (con’t ) • Type Error: a non well-defined operation on a variable in run time – Example: union in C union flexType { int i; float f; }; union flexType u; floa t x; … u.I = 10; x=x = ufu.f; … – Another example in C ? 6 Type Systems (con’t ) • Strongly Typed: All type errors are detected in compile or run time
    [Show full text]