Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#694 from boriel/feature/add_round_fun…
Browse files Browse the repository at this point in the history
…ction

feat: add ROUND library function
  • Loading branch information
boriel committed Jan 1, 2024
2 parents 2234740 + 153233a commit 49a3820
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/arch/zx48k/library/round.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
' ----------------------------------------------------------------
' This file is released under the MIT License
'
' Copyleft (k) 2024
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
' ----------------------------------------------------------------

#pragma once
#pragma push(case_insensitive)
#pragma case_insensitive = True

' ----------------------------------------------------------------
' function ROUND
'
' Parameters:
' n as Float: number to be rounded
' y as Ubyte: number of decimals
'
' Returns:
' A float rounded towards +/- infinity with the numbers of decimals
' requested.
' ----------------------------------------------------------------
FUNCTION Round(n as Float, decimals as UByte = 0) AS Float
DIM tmp as Float
DIM d10 as Float = 10^decimals
IF n >= 0 THEN
LET tmp = INT(n * d10 + 0.5)
ELSE
LET tmp = INT(n * d10 - 0.5)
END IF
RETURN tmp / d10
END FUNCTION


#pragma pop(case_insensitive)
35 changes: 35 additions & 0 deletions src/arch/zxnext/library/round.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
' ----------------------------------------------------------------
' This file is released under the MIT License
'
' Copyleft (k) 2024
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
' ----------------------------------------------------------------

#pragma once
#pragma push(case_insensitive)
#pragma case_insensitive = True

' ----------------------------------------------------------------
' function ROUND
'
' Parameters:
' n as Float: number to be rounded
' y as Ubyte: number of decimals
'
' Returns:
' A float rounded towards +/- infinity with the numbers of decimals
' requested.
' ----------------------------------------------------------------
FUNCTION Round(n as Float, decimals as UByte = 0) AS Float
DIM tmp as Float
DIM d10 as Float = 10^decimals
IF n >= 0 THEN
LET tmp = INT(n * d10 + 0.5)
ELSE
LET tmp = INT(n * d10 - 0.5)
END IF
RETURN tmp / d10
END FUNCTION


#pragma pop(case_insensitive)

0 comments on commit 49a3820

Please sign in to comment.