Difference between revisions of "Scientific notation"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Odin
(6 intermediate revisions by 2 users not shown)
Line 5: Line 5:
  
  
* '''E''' denotes [[SINGLE]] precision accuracy and '''D''' denotes [[DOUBLE]] precision accuracy in Qbasic.
+
* '''E''' denotes [[SINGLE]] precision accuracy and '''D''' denotes [[DOUBLE]] precision accuracy in Qbasic. D and E are considered numbers!
 
* To translate the notation, multiply the number preceding the letter by the value of 10 raised to the power following the letter.  
 
* To translate the notation, multiply the number preceding the letter by the value of 10 raised to the power following the letter.  
* '''Note:''' Naturally calculating the value in Qbasic would return the same value expression!
+
* [[PRINT USING]] can display the normal numerical values. You will have to use less digits than the real value.
 +
* '''Note:''' Naturally numerically calculating the value in Qbasic would return the same value!
 +
* [[INPUT]] WILL accept the letter E with [[SINGLE]] or [[DOUBLE]] variables while D can only be used with [[DOUBLE]] variables.
  
  
''Example 1:'' +2.184D+3 means to multiply 2.184 by 1,000 (1,000 is 10 raised to the third power, or 10 [[^]] 3 ).
+
''Sample 1:'' +2.184D+3 means to multiply 2.184 by 1,000 (1,000 is 10 raised to the third power, or 10 [[^]] 3 ).
 
: To multiply by 10 raised to a positive power, just move the decimal point to the right by 3.
 
: To multiply by 10 raised to a positive power, just move the decimal point to the right by 3.
 
: The result is 2184 in DOUBLE accuracy.
 
: The result is 2184 in DOUBLE accuracy.
  
''Example 2:'' -5.412D-2 is negative 5.412 times .01 (10 raised to the -2 power or 10 [[^]] -2 ).
+
''Sample 2:'' -5.412D-2 is negative 5.412 times .01 (10 raised to the -2 power or 10 [[^]] -2 ).
 
: To multiply a number by 10 raised to a negative power, just move the decimal point to the left by 2.
 
: To multiply a number by 10 raised to a negative power, just move the decimal point to the left by 2.
 
: The result is -.05412 in DOUBLE accuracy.
 
: The result is -.05412 in DOUBLE accuracy.
  
''Example 3:'' 3.07E+12 is a positive 3.07 times 1,000,000,000,000 (10 raised to the 12 power or 10 [[^]] 12).
+
''Sample 3:'' 3.07E+12 is a positive 3.07 times 1,000,000,000,000 (10 raised to the 12 power or 10 [[^]] 12).
 
: To multiply a number by 10 raised to a positive power, just move the decimal point to the right by 12.
 
: To multiply a number by 10 raised to a positive power, just move the decimal point to the right by 12.
 
: The result is 3,070,000,000,000 in SINGLE accuracy.
 
: The result is 3,070,000,000,000 in SINGLE accuracy.
 +
 +
 +
''Example:'' A string function that displays extremely small or large exponential decimal values.
 +
{{CodeStart}} '' ''
 +
num# = -2.34D-15
 +
{{Cl|PRINT}} num#
 +
{{Cl|PRINT}} StrNum$(num#)
 +
{{Cl|END}}
 +
 +
{{Cl|FUNCTION}} StrNum$ (n#)
 +
value$ = {{Cl|UCASE$}}({{Cl|LTRIM$}}({{Cl|STR$}}(n#)))
 +
Xpos% = {{Cl|INSTR}}(value$, "D") + {{Cl|INSTR}}(value$, "E")  'only D or E can be present
 +
{{Cl|IF}} Xpos% {{Cl|THEN}}
 +
  expo% = {{Cl|VAL}}({{Cl|MID$}}(value$, Xpos% + 1))
 +
  {{Cl|IF}} {{Cl|VAL}}(value$) < 0 {{Cl|THEN}}
 +
    sign$ = "-": valu$ = {{Cl|MID$}}(value$, 2, Xpos% - 2)
 +
  {{Cl|ELSE}} valu$ = {{Cl|MID$}}(value$, 1, Xpos% - 1)
 +
  {{Cl|END IF}}
 +
  dot% = {{Cl|INSTR}}(valu$, "."): L% = {{Cl|LEN}}(valu$)
 +
  {{Cl|IF}} expo% > 0 {{Cl|THEN}} add$ = {{Cl|STRING$}}(expo% - (L% - dot%), "0")
 +
  {{Cl|IF}} expo% < 0 {{Cl|THEN}} min$ = {{Cl|STRING$}}({{Cl|ABS}}(expo%) - (dot% - 1), "0"): DP$ = "."
 +
  {{Cl|FOR...NEXT|FOR}} n = 1 {{Cl|TO}} L%
 +
    {{Cl|IF}} {{Cl|MID$}}(valu$, n, 1) <> "." {{Cl|THEN}} num$ = num$ + {{Cl|MID$}}(valu$, n, 1)
 +
  {{Cl|NEXT}}
 +
{{Cl|ELSE}} StrNum$ = value$: {{Cl|EXIT FUNCTION}}
 +
{{Cl|END IF}}
 +
StrNum$ = sign$ + DP$ + min$ + num$ + add$
 +
{{Cl|END FUNCTION}} '' ''
 +
{{CodeEnd}}
 +
{{small|Code by Ted Weissgerber}}
 +
{{OutputStart}}
 +
-2.34D-15
 +
-.00000000000000234
 +
{{OutputEnd}}
  
  

Revision as of 18:20, 4 March 2019

Scientific notation or exponential notation is used to express very large or small numerical values by SINGLE or DOUBLE accuracy.


Usage: -9.7587E+04 or 4.6545D-9


  • E denotes SINGLE precision accuracy and D denotes DOUBLE precision accuracy in Qbasic. D and E are considered numbers!
  • To translate the notation, multiply the number preceding the letter by the value of 10 raised to the power following the letter.
  • PRINT USING can display the normal numerical values. You will have to use less digits than the real value.
  • Note: Naturally numerically calculating the value in Qbasic would return the same value!
  • INPUT WILL accept the letter E with SINGLE or DOUBLE variables while D can only be used with DOUBLE variables.


Sample 1: +2.184D+3 means to multiply 2.184 by 1,000 (1,000 is 10 raised to the third power, or 10 ^ 3 ).

To multiply by 10 raised to a positive power, just move the decimal point to the right by 3.
The result is 2184 in DOUBLE accuracy.

Sample 2: -5.412D-2 is negative 5.412 times .01 (10 raised to the -2 power or 10 ^ -2 ).

To multiply a number by 10 raised to a negative power, just move the decimal point to the left by 2.
The result is -.05412 in DOUBLE accuracy.

Sample 3: 3.07E+12 is a positive 3.07 times 1,000,000,000,000 (10 raised to the 12 power or 10 ^ 12).

To multiply a number by 10 raised to a positive power, just move the decimal point to the right by 12.
The result is 3,070,000,000,000 in SINGLE accuracy.


Example: A string function that displays extremely small or large exponential decimal values.

num# = -2.34D-15 PRINT num# PRINT StrNum$(num#) END FUNCTION StrNum$ (n#) value$ = UCASE$(LTRIM$(STR$(n#))) Xpos% = INSTR(value$, "D") + INSTR(value$, "E") 'only D or E can be present IF Xpos% THEN expo% = VAL(MID$(value$, Xpos% + 1)) IF VAL(value$) < 0 THEN sign$ = "-": valu$ = MID$(value$, 2, Xpos% - 2) ELSE valu$ = MID$(value$, 1, Xpos% - 1) END IF dot% = INSTR(valu$, "."): L% = LEN(valu$) IF expo% > 0 THEN add$ = STRING$(expo% - (L% - dot%), "0") IF expo% < 0 THEN min$ = STRING$(ABS(expo%) - (dot% - 1), "0"): DP$ = "." FOR n = 1 TO L% IF MID$(valu$, n, 1) <> "." THEN num$ = num$ + MID$(valu$, n, 1) NEXT ELSE StrNum$ = value$: EXIT FUNCTION END IF StrNum$ = sign$ + DP$ + min$ + num$ + add$ END FUNCTION

Code by Ted Weissgerber

-2.34D-15 -.00000000000000234


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By usage
Main WIKI Page