Difference between revisions of "Scientific notation"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
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.  
 
* [[PRINT USING]] can display the normal numerical values. You may have to use less digits.
 
* [[PRINT USING]] can display the normal numerical values. You may have to use less digits.
Line 32: Line 32:
  
 
{{Cl|FUNCTION}} StrNum$ (n#)
 
{{Cl|FUNCTION}} StrNum$ (n#)
value$ = {{Cl|LTRIM$}}({{Cl|STR$}}(n#))
+
value$ = {{Cl|UCASE$}}({{Cl|LTRIM$}}({{Cl|STR$}}(n#))}
Xpos% = {{Cl|INSTR}}(value$, "D") + {{Cl|INSTR}}(value$, "E")
+
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$}}(value$, Xpos% + 1))
Line 51: Line 51:
 
{{Cl|END FUNCTION}} '' ''
 
{{Cl|END FUNCTION}} '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
 +
{{small|Code by Ted Weissgerber}}
 
{{OutputStart}}
 
{{OutputStart}}
 
  -2.34D-15
 
  -2.34D-15

Revision as of 23:43, 11 November 2010

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 may have to use less digits.
  • Note: Naturally numerically calculating the value in Qbasic would return the same value!


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