ROUND: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:_ROUND}}
{{DISPLAYTITLE:_ROUND}}
The [[_ROUND]] function rounds to the closest [[INTEGER]], [[LONG]] or [[_INTEGER64]] numerical value. (If the value ends with .5, it rounds instead to the closest EVEN integer value.)
The [[_ROUND]] function rounds [[SINGLE]], [[DOUBLE]] and [[_FLOAT]] decimal point numbers to the nearest [[INTEGER]], [[LONG]] or [[_INTEGER64]] value with one exception. If the fraction of the decimal point number is exactly .5, then it is '''always''' rounded to the nearest '''even''' integer value.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|value}} = [[_ROUND]]({{Parameter|number}})
: {{Parameter|value}} = [[_ROUND]]({{Parameter|expression}})
 
 
{{PageParameters}}
* {{Parameter|expression}} is any [[Variable Types|type]] of literal or variable numerical value or mathematical calculation.




{{PageDescription}}
{{PageDescription}}
* Can round [[SINGLE]], [[DOUBLE]] or [[_FLOAT]] floating decimal point parameter values.
;* Note: As given values may be positive or negative and the rounding directions are opposite in each case, we don't speak of rounding up or down. That's elementary school terminology when kids don't know of negative numbers yet.
* Can be used when numerical values exceed the limits of [[CINT]] or [[CLNG]].
:* Instead we speak of rounding towards zero or rounding towards infinity, which exactly describes the rounding direction for both, positive and negative numbers.
* If the number to be rounded is less than 0.5, then it rounds down to the closest integer value.
* Fractions greater than .5 are rounded to the next integer number towards infinity
* If the number to be rounded is greater than 0.5, then it rounds up to the closest integer value.
* Fractions lower than .5 are rounded to the next integer number towards zero.
* If the number to be rounded ends in 0.5, rounding is done to the closest even integer value.
* Fractions exactly .5 are rounded to the nearest '''even''' integer number.
* This function practically superseds [[CINT]] and [[CLNG]], as it can handle all floating point types and return appropriate integer types, while the mentioned legacy commands imposed limits on their inputs and results.
* This function is automatically applied (in the background) to decimal point values, which are given to SUBs or FUNCTIONs requiring [[_INTEGER64]]s as arguments.
 
 
{{PageAvailability}}
<!-- QB64 = a version or none, QBPE = a version or all, Platforms = yes or no -->
<gallery widths="48px" heights="48px" mode="nolines">
File:Qb64.png|'''all'''
File:Qbpe.png|'''all'''
File:Apix.png
File:Win.png|'''yes'''
File:Lnx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
<!-- additional availability notes go below here -->




''Example:'' Displays how QB64 rounds to the closest even integer value.
{{PageExamples}}
;Example: Shows how _ROUND rounds values depending on the current fraction.
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Cl|_ROUND}}(0.5)
{{Cl|WIDTH}} {{Text|50|#F580B1}}, {{Text|35|#F580B1}}
{{Cl|PRINT}} {{Cl|_ROUND}}(1.5)
{{Cl|FOR}} f = {{Text|-3.5|#F580B1}} {{Cl|TO}} {{Text|3.5|#F580B1}}
{{Cl|PRINT}} {{Cl|_ROUND}}(2.5)
    {{Cl|IF}} f &amp;lt; {{Text|0|#F580B1}} {{Cl|THEN}}
{{Cl|PRINT}} {{Cl|_ROUND}}(3.5)
        down$ = {{Text|<nowiki>"towards infinity"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Cl|_ROUND}}(4.5)
        up$ = {{Text|<nowiki>"towards zero"</nowiki>|#FFB100}}
{{Cl|PRINT}} {{Cl|_ROUND}}(5.5)
    {{Cl|ELSE}}
        down$ = {{Text|<nowiki>"towards zero"</nowiki>|#FFB100}}
        up$ = {{Text|<nowiki>"towards infinity"</nowiki>|#FFB100}}
    {{Cl|END IF}}
    {{Cl|PRINT}} f - {{Text|0.01|#F580B1}}, {{Cl|_ROUND}}(f - {{Text|0.01|#F580B1}}), down$
    {{Cl|PRINT}} f, {{Cl|_ROUND}}(f), {{Text|<nowiki>"to nearest even"</nowiki>|#FFB100}}
    {{Cl|PRINT}} f + {{Text|0.01|#F580B1}}, {{Cl|_ROUND}}(f + {{Text|0.01|#F580B1}}), up$
    {{Cl|PRINT}}
{{Cl|NEXT}} f
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}0
2
2
4
4
6
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[INT]], [[CINT]]
* [https://qb64phoenix.com/forum/showthread.php?tid=3661 Featured in our "Keyword of the Day" series]
* [[FIX]], [[CLNG]]
* [[CINT]], [[_CEIL]]
* [[CLNG]], [[CSNG]], [[CDBL]]
* [[INT]], [[FIX]]




{{PageNavigation}}
{{PageNavigation}}

Revision as of 22:18, 7 May 2025

The _ROUND function rounds SINGLE, DOUBLE and _FLOAT decimal point numbers to the nearest INTEGER, LONG or _INTEGER64 value with one exception. If the fraction of the decimal point number is exactly .5, then it is always rounded to the nearest even integer value.


Syntax

value = _ROUND(expression)


Parameters

  • expression is any type of literal or variable numerical value or mathematical calculation.


Description

Note
  • As given values may be positive or negative and the rounding directions are opposite in each case, we don't speak of rounding up or down. That's elementary school terminology when kids don't know of negative numbers yet.
  • Instead we speak of rounding towards zero or rounding towards infinity, which exactly describes the rounding direction for both, positive and negative numbers.
  • Fractions greater than .5 are rounded to the next integer number towards infinity
  • Fractions lower than .5 are rounded to the next integer number towards zero.
  • Fractions exactly .5 are rounded to the nearest even integer number.
  • This function practically superseds CINT and CLNG, as it can handle all floating point types and return appropriate integer types, while the mentioned legacy commands imposed limits on their inputs and results.
  • This function is automatically applied (in the background) to decimal point values, which are given to SUBs or FUNCTIONs requiring _INTEGER64s as arguments.


Availability


Examples

Example
Shows how _ROUND rounds values depending on the current fraction.
WIDTH 50, 35
FOR f = -3.5 TO 3.5
    IF f &lt; 0 THEN
        down$ = "towards infinity"
        up$ = "towards zero"
    ELSE
        down$ = "towards zero"
        up$ = "towards infinity"
    END IF
    PRINT f - 0.01, _ROUND(f - 0.01), down$
    PRINT f, _ROUND(f), "to nearest even"
    PRINT f + 0.01, _ROUND(f + 0.01), up$
    PRINT
NEXT f


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link