How to truncate decimal points

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

How to truncate decimal points

Post by zeynel1 »

In the following program I use the acceleration due to gravity as g=9.8. Then I calculate g1 at a certain altitude with formula g1=g*(1-(2*h/R)).

The formula returns g1 with 8 decimal points: g1=9.79692356. Is there a way to truncate the returned value so that I have g1=9.7 ?

Thanks.

Code: Select all

      REM g at the surface meters/second^2
      g=9.8067
      REM R=Earth radius in meters
      R=6371000
      REM h=altitude
      h=0
      REM Cavendish's value for density of Earth: 5448 kg/m^3
      Delta=5448
      PRINT "Using Cavendish's value for density of earth: 5448 kg/m^3"

      FOR h=0 TO 4000 STEP 1000
        g1=g*(1-(2*h/R))
        PRINT "Altitude is: ";h; " meters"
        PRINT "g1 is: ";g1

        G_Delta=(3*g1)/(4*PI*(R+h)*Delta)
        PRINT "G_Delta=";G_Delta
      NEXT h
      END
Hated Moron

Re: How to truncate decimal points

Post by Hated Moron »

zeynel1 wrote: Sat 28 May 2022, 07:08 The formula returns g1 with 8 decimal points: g1=9.79692356. Is there a way to truncate the returned value so that I have g1=9.7 ?
Are you sure you want truncation rather than rounding? That's somewhat unusual, normally one would round to the nearest value (9.8). For example:

Code: Select all

      g1 = 9.79692356
      @% = &2010A
      PRINT g1
or using a library:

Code: Select all

      INSTALL @lib$+"fnusing"
      g1 = 9.79692356
      PRINT FNusing("##.#", g1)
If you really want to truncate, you would need to do something like:

Code: Select all

      g1 = 9.79692356
      PRINT INT(g1 * 10) / 10
But if you do that beware 'unexpected' results caused by internal conversion from decimal to binary and back to decimal again. You may also get different results when running on an x86 CPU (80-bits precision internally) and an ARM CPU (64-bits precision).
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: How to truncate decimal points

Post by zeynel1 »

Great, thanks.

You are right, I need to round not truncate.

I did not understand the first code but the second worked for me.

Thanks again.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: How to truncate decimal points

Post by KenDown »

However, if you did want to truncate, I'm surprised no one has mentioned the convert to string option.

Code: Select all

      a=1/3
      a$=STR$a
      b$=LEFT$(a$,3)
      PRINTb$
      b=VALb$
      PRINTb
That would work with integer values up to 9 (ie. 9.99999). If the integer part was likely to go above 9 then you would have to write a more complicated routine that worked out where the decimal point was and alter LEFT$( accordingly.

I need hardly say that this is a slower method than the other methods given above.
nvingo
Posts: 41
Joined: Sat 28 May 2022, 22:40

Re: How to truncate decimal points

Post by nvingo »

I wrote up this reply yesterday, but had to wait for forum authentication which went to my spam folder.

Use INT and *xx /xx
for one decimal place xx=10, for 2 decimal places xx=100 etc.

Replace your expression
G_Delta=(3*g1)/(4*PI*(R+h)*Delta)
with
G_Delta=INT(10*(3*g1)/(4*PI*(R+h)*Delta))/10
So g1=9.79692356
*10 gives g1=97.9692356
INT gives g1=97
/10 gives g1=9.7
If you want to round to nearest add 0.5 within the INT
eg.
G_Delta=INT(10*(3*g1)/(4*PI*(R+h)*Delta)+.5)/10

Broadly matches the reply from Hated Moron.

In case doing it numerically is less efficient, there is another way (truncate only):
G$=STR$(G_Delta) : G_Delta=VAL(LEFT$(G$,1+INSTR(G$,".")))

For more on the @% system variable for format control, see the BBC BASIC manual entry here: https://www.bbcbasic.co.uk/bbcwin/manua ... rintformat
Started using BASIC circa 1981 with CP/M, Video Genie, Sinclair ZX81, Acorn Atom, and progressed with ZX Spectrum, BBC Micro and Sinclair QL, Cambridge Z88, DOS, Windows. Wrote A-level project using school's BBC Micro with dual 800K floppy drive.
Hated Moron

Re: How to truncate decimal points

Post by Hated Moron »

nvingo wrote: Sun 29 May 2022, 17:00 In case doing it numerically is less efficient, there is another way (truncate only):
G$=STR$(G_Delta) : G_Delta=VAL(LEFT$(G$,1+INSTR(G$,".")))
On a simple measurement, the string method is approximately three times slower than the numeric method. Add to that an undesirable dependence on the value of @%, and that truncation is fundamentally a numeric operation, it doesn't really have any benefits.