CINT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
The [[CINT]] function rounds decimal point numbers up or down to the nearest [[INTEGER]] value. (IF the number ends in exactly 0.5, then it is rounded to the nearest EVEN integer value.)
The [[CINT]] function rounds decimal point numbers to the nearest [[INTEGER]] 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.




Line 7: Line 7:


{{PageParameters}}
{{PageParameters}}
* {{Parameter|expression}} is any [[TYPE]] of literal or variable numerical value or mathematical calculation.
* {{Parameter|expression}} is any [[Variable Types|type]] of literal or variable numerical value or mathematical calculation.




{{PageDescription}}
{{PageDescription}}
* Values greater than .5 are rounded up. Values lower than .5 are rounded down.
;* 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.
* Values that end with exactly .5 are rounded to the nearest even integer.
:* Instead we speak of rounding towards zero or rounding towards infinity, which exactly describes the rounding direction for both, positive and negative numbers.
* ''Warning:'' Since [[CINT]] is used for integer values, the input values cannot exceed 32767 to -32768!
* Fractions greater than .5 are rounded to the next integer number towards infinity
* Use [[CLNG]] for [[LONG]] integer values exceeding [[INTEGER]] limitations.
* Fractions lower than .5 are rounded to the next integer number towards zero.
* Note: When decimal point values are given to BASIC functions requiring [[INTEGER]]s the value will be [[CINT]]ed.
* Fractions exactly .5 are rounded to the nearest '''even''' integer number.
* This function is automatically applied (in the background) to decimal point values, which are given to SUBs or FUNCTIONs requiring [[INTEGER]]s as arguments.
;* Warning: Since [[CINT]] is used for [[INTEGER]] values, the input values cannot exceed -32768 to 32767.
:* Use [[CLNG]] for [[LONG]] integer values exceeding [[INTEGER]] limitations.
 
 
{{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 -->




{{PageExamples}}
{{PageExamples}}
''Example:'' Shows how CINT rounds values up or down as in "bankers' rounding".
;Example: Shows how CINT rounds values depending on the current fraction.
{{CodeStart}}
{{CodeStart}}
a% = {{Cl|CINT}}(1.49): b% = {{Cl|CINT}}(1.50): c = 11.5
{{Cl|WIDTH}} {{Text|50|#F580B1}}, {{Text|35|#F580B1}}
{{Cl|COLOR}} c: {{Cl|PRINT}} a%, b%, c
{{Cl|FOR}} f = {{Text|-3.5|#F580B1}} {{Cl|TO}} {{Text|3.5|#F580B1}}
    {{Cl|IF}} f &amp;lt; {{Text|0|#F580B1}} {{Cl|THEN}}
        down$ = {{Text|<nowiki>"towards infinity"</nowiki>|#FFB100}}
        up$ = {{Text|<nowiki>"towards zero"</nowiki>|#FFB100}}
    {{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|CINT}}(f - {{Text|0.01|#F580B1}}), down$
    {{Cl|PRINT}} f, {{Cl|CINT}}(f), {{Text|<nowiki>"to nearest even"</nowiki>|#FFB100}}
    {{Cl|PRINT}} f + {{Text|0.01|#F580B1}}, {{Cl|CINT}}(f + {{Text|0.01|#F580B1}}), up$
    {{Cl|PRINT}}
{{Cl|NEXT}} f
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}{{Text|1      2      11.5|red}}
{{OutputEnd}}





Revision as of 22:17, 7 May 2025

The CINT function rounds decimal point numbers to the nearest INTEGER 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% = CINT(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 is automatically applied (in the background) to decimal point values, which are given to SUBs or FUNCTIONs requiring INTEGERs as arguments.
Warning
  • Since CINT is used for INTEGER values, the input values cannot exceed -32768 to 32767.
  • Use CLNG for LONG integer values exceeding INTEGER limitations.


Availability


Examples

Example
Shows how CINT 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, CINT(f - 0.01), down$
    PRINT f, CINT(f), "to nearest even"
    PRINT f + 0.01, CINT(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