Long (LONGMATH) variables may be used the same as short variables. The syntax is the same. A few limitations apply (see below).

Similar documents
Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Introduction to QuickCalc BASIC. QUICKCALC Sample Programs. QUICKCALC BASIC vs IBM BASIC. Note: Please read the User License Agreement, below.

3.1 DATA REPRESENTATION (PART C)

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Math Glossary Numbers and Arithmetic

Number Systems. Both numbers are positive

>>> * *(25**0.16) *10*(25**0.16)

Outline. Data and Operations. Data Types. Integral Types

Our Strategy for Learning Fortran 90

Graphics calculator instructions

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Advanced Features in QuickCalc

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Divide: Paper & Pencil

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

1.1 Review of Place Value

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Math 1 Variable Manipulation Part 2 Exponents & Roots

VBScript: Math Functions

ECE232: Hardware Organization and Design

Programming Language 2 (PL2)

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Place Value. Verbal Form: 30,542 = Thirty thousand, five hundred forty-two. (Notice we don t use the word and.)

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

hp calculators HP 9s Basic Arithmetic Practice Doing Arithmetic

Fundamentals: Expressions and Assignment

Chapter 2: Number Systems

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

CHAPTER 5: Representing Numerical Data

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Programming in C++ 6. Floating point data types

SPARK-PL: Introduction

KNOWLEDGE OF NUMBER SENSE, CONCEPTS, AND OPERATIONS

Built-in Types of Data

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

EXAMPLE 1. Change each of the following fractions into decimals.

ANSI C Programming Simple Programs

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Dr Richard Greenaway

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Introduction to TURING

Types and Expressions. Chapter 3

6th Grade Arithmetic (with QuickTables)

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Chapter 4 Section 2 Operations on Decimals

Florida Math 0018 Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

Table of Contents. PREFACE... vii CONVENTIONS... vii HOW TO USE THIS MANUAL... vii Further Information...viii

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

REVIEW. The C++ Programming Language. CS 151 Review #2

Chapter 2 Data Representations

Fixed-Point Math and Other Optimizations

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Full file at

Fundamentals of Programming Session 8

MATHEMATICAL / NUMERICAL FUNCTIONS

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Arithmetic for Computers. Hwansoo Han

hp calculators HP 35s Using Algebraic Mode Calculation modes Functions of a single number in algebraic A simple example in algebraic

Computer System and programming in C

A. Incorrect! To simplify this expression you need to find the product of 7 and 4, not the sum.

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Data Representations & Arithmetic Operations

Exponents. Although exponents can be negative as well as positive numbers, this chapter will only address the use of positive exponents.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Programming Using C Homework 4

Module 2: Computer Arithmetic

Julia Calculator ( Introduction)

Internal Data Representation

FLOATING POINT NUMBERS

Chapter 3: Operators, Expressions and Type Conversion

unused unused unused unused unused unused

Downloaded from Chapter 2. Functions

The C++ Language. Arizona State University 1

Level ISA3: Information Representation

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Floating Point Arithmetic

Math 10- Chapter 2 Review

Lecture 2 FORTRAN Basics. Lubna Ahmed

Objectives. Connecting with Computer Science 2

Lesson 1: Arithmetic Review

9. Elementary Algebraic and Transcendental Scalar Functions

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

FLOATING POINT NUMBERS

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 2. Outline. Simple C++ Programs

Transcription:

Working with Long Numbers. Long Variables Constants You define a long variable with the LONG statement, which works similar to the DIM statement. You can define long variables and dimension long variable arrays in the same statement: LONG a, b, c(5,2), etc. Long names and arrays cannot duplicate short names and may not end with $. Long (LONGMATH) variables may be used the same as short variables. The syntax is the same. A few limitations apply (see below). Note: You can, optionally, choose to run your program with LONGMATH numbers and variables only. (See Working With Long Numbers Only, below.) Numeric constants entered into your program, typed on the command line, used in DATA statements, or input from the console or a file, may be either long or short. A number is presumed to be and treated as short unless: it is more than 16 (significant) digits long, it has an exponent out of the range of DOUBLE numbers (approximately +/- 308) Numbers outside this range may only be assigned to LONGMATH variables. If they are used in an arithmetic operation, the operation will be performed using LONGMATH routines. Numbers entered, short or long, are maintained as numeric strings until they are assigned to a variable or used in an arithmetic operation or function. They will then be converted to match the type of their destination variable or the required type for the function. If they are required to be short, they will be converted automatically. Note: this may cause an error if the exponent is too large. Numbers can be forced to be long or short with the functions LONG (x) and SHORT (x). LONGPI and LONGE represent long versions of the constants and e. These values are calculated at the current floating-point length when they are referenced, and saved for subsequent use. If a longer value is requested, it will be recalculated and saved. If a shorter or same length value is requested, the saved value will be used, rounded to the desired length. Integer vs. Floating-Point Numbers.

There is no difference between the two in terms of how the numbers are stored. Integer and Floating-Point refer to the way operations are performed on these numbers. Integers are simply numbers with nothing to the right of the decimal point. All digits to the left of the decimal point are used in integer arithmetic. If the number was floating-point (i.e., had digits to the right of the decimal), the fractional digits will be ignored. Integer division always produces a quotient and a remainder. Trig functions, EXP, LOG, SQR, etc. are not valid in integer mode. Floating-point numbers have a fixed number of total significant digits, and an exponent. Division is carried out to the specified floating-point length with no remainder. All answers are rounded to the current floating-point length. Both formats are stored the same, and may be used interchangeably. A long integer with a lot of trailing zeros will be stored without those zeros, e.g., 2.5E+5000. Switching between integer and floating-point modes does not change numbers which have already been stored. Changing the floating-point length also does not affect existing numbers only the results of subsequent calculations. Range and Size of Long Numbers. Arithmetic Long integers are limited to 1 million digits. Floating point numbers are limited by the value you set for the floating point length (use the FLOAT statement). You may make them any size you like (up to 1 million digits) subject to the memory on your computer and your patience. Huge numbers can take a long time to calculate, even with the highly-optimized routines used in QuickCalc. The range of long numbers is exponents up to ± 1,000,000. Note: the number 1E1000000 still takes only 4 bytes to store, but 1E1000000 1 is.9999 (1 million 9 s). All floating point numbers are rounded to FLOATING_PT_LENGTH significant digits. Math functions are performed in the same sequence and priority as with regular numbers. Operations like +, -, *, /, %, and ^ operate on long and regular numbers. If either argument is long, the other is converted to long and a long operation is performed, yielding a long result. If the resulting value is assigned to a short variable, it will be converted down (see Conversion ). The sequence that operations appear in the program statement determines whether the operations are long or short. This can cause unexpected results if you code the statements incorrectly.

Examples: 2/3 => 0.66666666666666667 (short) 2/(LONG(3)) => 0.6666666666666666666666666666666666666 (long) longvar = SQR (2) => 1.4142135623730951 (short, converted to long) longvar = SQR (LONG (2)) => 1.414213562373095048801688724280785696. (long) longvar = 2: longvar=sqr(longvar) => 1.414213562373095048801688724280785696. (long) 2 is converted to long & assigned to longvar. Then a long SQR is done. longvar = 88888888 * 1e13: PRINT longvar result = 888888880000000070000 (error caused by binary rounding in DOUBLE arithmetic. longvar = LONG (88888888) * 1e13: PRINT longvar result = 888888880000000000000 (no error) Calculating r 2 depends on the order: (assume r is short) LONGPI * r * r (does all multiplies in LONG) r * r * LONGPI (does the first multiply as short). More examples and a more detailed discussion are provided in the section Number Conversion and Assignment These examples show how you may not get the precision you expected if you code it incorrectly. You can, of course, use parentheses to force the order of operations, and the LONG (x) and SHORT (x) functions can force conversion. The result of the calculation is converted (if possible) to match the destination variable. If you wish to avoid the confusion and issues described above regarding which numbers are long and short, and how and when they are converted, you can choose to work with long numbers only (see Working With Long Numbers Only, below). Comparisons You may mix long and short numbers or variables in comparison operations. The same rules apply. When a long and a short number are compared, the short is first converted to a long. The comparison may not be exactly as you expect (see the example above using 88888888 * 1e13.

Logic Operations Functions Printing When used in a logic operation, long numbers are treated just like short ones. Zero=false, non-zero = true. Special, highly-optimized functions are provided for SQR, LOG, LOG10, EXP, EXP10, SIN, COS, TAN, ATN, ACOT, FACTORIAL, ROOT and raise to power (^). These routines perform long arithmetic to the precision specified in the FLOAT statement. They are automatically called if the argument is long. Pay attention to the type or argument being passed. SIN (180 / PI * angle), where angle is short, gives a short result. Even if angle is long, 180/PI is a short calculation. For maximum precision, specify SIN (180/LONGPI * angle) or use the DEGREES statement with SIN (LONG (angle) (see notes, above, on arithmetic.) These functions (with the exception of FACTORIAL) are not valid when you are in INTEGER mode. Functions operating on long arguments return long results. Functions which expect only short arguments will convert long to short, if possible. Long numbers are printed as if they were long strings. They are printed to multiple lines of the console output. If you are printing to a file, each line will be as long as will fit in the file s record length. Long numbers may be formatted with a PRINT USING statement. Use the format string www.pppc, where www is the total width of the output string (255 characters maximum). decimal point (optional). If present, a decimal point is printed, even if there are no digits to the right of the decimal point. ppp c is the precision (optional), i.e., the number of digits to the right of the decimal point. It may be from 0 to www 3. (optional) means place a comma between every 3 digits left of the decimal.

Long numbers formatted in this way will have their decimal points aligned if they use the same format string. Example: will print: LONG a, b, c, d a = LONGPI * 1e10 b = LONGE / 1e10 c = -2.5e7 d = 0 PRINT USING "40.20c", a PRINT USING "40.20c", b PRINT USING "40.20c", c PRINT USING "40.20c", d 31,415,926,535.89793238462643383280 0.00000000027182818285-25,000,000.00000000000000000000 0.00000000000000000000 Note: Do not use this type of format string for DOUBLE variables. Do not assign it to PRINTFORMAT$$. Notes: Values printed this way will be rounded to the rightmost printed digit. Values which are too large will be formatted in exponential notation. Values which are too small will be printed as zero. There will always be at least one digit or zero left of the decimal point. When commas are used, you cannot print as many digits to the left of the decimal point, (obviously). This type of format string can also be used with the STR$ function. See the section Number Conversion and Assignment for more, and for how long numbers are handled in the WRITE statement. Converting between Long and Short. (see the section Number Conversion and Assignment ) Working With Long Numbers Only. If you wish to avoid the confusion and issues described above regarding which numbers are long and short, and how and when they are converted, you can choose to work with long numbers only. To initiate this mode, specify the statement LONG "ALL" (The quotes around ALL are required.)

This is usually done at the start of the program. It affects all subsequent variables and calculations. The statement remains in effect until the end of the program, the start of the next program, or until you specify LONG "ALL", OFF. When this mode is in effect, all new variables are assumed to be LONGMATH, all arrays dimensioned are created as LONGMATH all constants entered or read are converted to LONGMATH, all arithmetic is performed with long calculations, all functions do LONGMATH calculations and return long results, logic operations and comparisons are performed using long calculations, PI and EULER (constants) return LONGPI and LONGE at the current floating-point length. Things to keep in mind when using this mode: Long (LONGMATH) calculations take more time than short (DOUBLE) calculations. Your program will run longer because things like indexes and counters (e.g., in FOR statements) will all be using long calculations. Long numbers must be formatted differently in PRINT USING, and STR$ (see above). You can use the SHORT function to convert a number to a DOUBLE, if required (e.g., PRINT SHORT (x)). You can turn this mode off at any time using LONG ALL OFF. There is a limit to the number of LONGMATH variables, and if all your variables are LONGMATH, you could exceed this number. (See Maximum Number of Long Variables, below) Maximum Number of Long Variables. By default, you may have up to 100 long number (LONGMATH) variables. This includes array elements. Each long number requires 300 bytes plus storage dynamically allocated to hold the number itself. You may change the number of LONGMATH variables with the Customize dialog. Storage Requirements. LONGMATH Numbers are stored in structures. The structure contains information about the number, and a buffer to hold the number if it is less then 512 digits. If the number is longer than 512, additional memory is dynamically allocated to hold the number. Each 4-byte word of memory holds 8 digits.

When a LONGMATH variable is defined, it is not assigned a structure until a value is assigned to it. Unused variables have a value of zero. You can allocate a large array of LONGMATH variables and none of them will require a structure until the first time each is assigned a value. Long multiply and divide, plus power series functions like LOG, require additional buffers for temporary results. These buffers are allocated automatically as needed and freed when no longer needed. If any memory allocation fails, it will generate an error and cancel your program. Speed Long number (LONGMATH) operations take more time than short (DOUBLE) operations. Extremely long numbers can take a very long time for multiply, divide, SQR and power-series functions like SIN, COS, TAN, COT, ATN, ACOT, LOG[10], EXP[10], LONGPI, LONGE, FACTORIAL, ROOT and raise-to-power (^). Although the LONGMATH routines are perhaps the fastest available, you should not set the floating point length longer than you need. You can operate with numbers up to 1 million digits long, if required. These routines are optimized to perform the above calculations in approximately one second (on a 3 GHz processor) at a length of 10000 digits. Calculation time is approximately proportional to the square of the number of digits, so if a calculation takes one second at 10000 digits, it will take about 100 seconds at 100000 digits, etc. LONGPI at 1 million digits takes about 1 hour and 40 minutes on a 3GHz processor. You can use the Kill Program button to terminate a LONGMATH function that is taking too long. It will interrupt approximately 0.1 sec. after you click the button and will terminate the BASIC program without completing the calculation. The Step button will break into the program, but not until the BASIC statement terminates, which may be a long time.