Scientific notation: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (QBasic capitalisation)
Tag: visualeditor
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:


* '''E''' denotes [[SINGLE]] precision accuracy and '''D''' denotes [[DOUBLE]] precision accuracy in QBasic. D and E are considered numbers!
* '''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.
* [[PRINT USING]] can display the normal numerical values. You will have to use less digits than the real value.
* [[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!
* '''Note:''' Naturally numerically calculating the value in QBasic would return the same value!
Line 26: Line 26:


''Example:'' A string function that displays extremely small or large exponential decimal values.
''Example:'' A string function that displays extremely small or large exponential decimal values.
{{CodeStart}} '' ''
{{CodeStart}}
num# = -2.34D-15
num# = -2.34D-15
{{Cl|PRINT}} num#
{{Cl|PRINT}} num#
Line 36: Line 36:
Xpos% = {{Cl|INSTR}}(value$, "D") + {{Cl|INSTR}}(value$, "E")  'only D or E can be present
Xpos% = {{Cl|INSTR}}(value$, "D") + {{Cl|INSTR}}(value$, "E")  'only D or E can be present
{{Cl|IF}} Xpos% {{Cl|THEN}}
{{Cl|IF}} Xpos% {{Cl|THEN}}
   expo% = {{Cl|VAL}}({{Cl|MID$}}(value$, Xpos% + 1))
   expo% = {{Cl|VAL}}({{Cl|MID$ (function)|MID$}}(value$, Xpos% + 1))
   {{Cl|IF}} {{Cl|VAL}}(value$) < 0 {{Cl|THEN}}
   {{Cl|IF}} {{Cl|VAL}}(value$) < 0 {{Cl|THEN}}
     sign$ = "-": valu$ = {{Cl|MID$}}(value$, 2, Xpos% - 2)
     sign$ = "-": valu$ = {{Cl|MID$ (function)|MID$}}(value$, 2, Xpos% - 2)
   {{Cl|ELSE}} valu$ = {{Cl|MID$}}(value$, 1, Xpos% - 1)
   {{Cl|ELSE}} valu$ = {{Cl|MID$ (function)|MID$}}(value$, 1, Xpos% - 1)
   {{Cl|END IF}}
   {{Cl|END IF}}
   dot% = {{Cl|INSTR}}(valu$, "."): L% = {{Cl|LEN}}(valu$)
   dot% = {{Cl|INSTR}}(valu$, "."): L% = {{Cl|LEN}}(valu$)
Line 45: Line 45:
   {{Cl|IF}} expo% < 0 {{Cl|THEN}} min$ = {{Cl|STRING$}}({{Cl|ABS}}(expo%) - (dot% - 1), "0"): DP$ = "."
   {{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|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|IF}} {{Cl|MID$ (function)|MID$}}(valu$, n, 1) <> "." {{Cl|THEN}} num$ = num$ + {{Cl|MID$ (function)|MID$}}(valu$, n, 1)
   {{Cl|NEXT}}
   {{Cl|NEXT}}
{{Cl|ELSE}} StrNum$ = value$: {{Cl|EXIT FUNCTION}}
{{Cl|ELSE}} StrNum$ = value$: {{Cl|EXIT FUNCTION}}
{{Cl|END IF}}
{{Cl|END IF}}
StrNum$ = sign$ + DP$ + min$ + num$ + add$
StrNum$ = sign$ + DP$ + min$ + num$ + add$
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
{{OutputStart}}
{{OutputStart}}
  -2.34D-15
  -2.34D-15
Line 59: Line 59:




''See also:''
{{PageSeeAlso}}
 
* [[^|^ Exponential operator]]
* [[^|^ Exponential operator]]
* [[SINGLE]], [[DOUBLE]]
* [[SINGLE]], [[DOUBLE]]

Latest revision as of 00:45, 26 February 2023

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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link