---
url: /dyad/stable/stdlib/BlockComponents/tests/Math.Tests.RealToInteger.md
description: >-
  Dyad Math.Tests.RealToInteger docs.  Rounds constant and time-varying Real
  inputs to the nearest Integer.
---

# `Math.Tests.RealToInteger` {#Math.Tests.RealToInteger}

Rounds constant and time-varying Real inputs to the nearest Integer.

Connects a constant source with value 2.7 to a RealToInteger block and verifies that the output rounds to 3. A second RealToInteger block is driven by a sine wave (amplitude 3) so the rounded integer output steps through several values over time.

## Usage {#Usage}

`BlockComponents.Math.Tests.RealToInteger()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( c1\_{+}y, r2i\_{+}u \right) \\
0 &= \mathrm{connect}\left( sine\_{+}y, r2i\_{2\_{+}u} \right) \\
\mathtt{c1.y}\left( t \right) &= \mathtt{c1.k} \\
\mathtt{r2i.y}\left( t \right) &= round_int\left( \mathtt{r2i.u}\left( t \right) \right) \\
\mathtt{sine.y}\left( t \right) &= ifelse\left( \mathtt{sine.continuous}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right), \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + \mathtt{sine.start_time} \right) \right) \right) ~ \mathtt{sine.amplitude}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right) ~ \mathtt{sine.amplitude}, 0 \right) \right) + \mathtt{sine.offset} \\
\mathtt{r2i_2.y}\left( t \right) &= round_int\left( \mathtt{r2i_2.u}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Rounds constant and time-varying Real inputs to the nearest Integer.

Connects a constant source with value 2.7 to a RealToInteger block and verifies
that the output rounds to 3. A second RealToInteger block is driven by a sine
wave (amplitude 3) so the rounded integer output steps through several values
over time.
"""
test component RealToInteger
  "Constant source providing the Real input"
  c1 = BlockComponents.Sources.Constant(k = 2.7) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "RealToInteger block under test"
  r2i = BlockComponents.Math.RealToInteger() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 20, "x2": 270, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping negative and positive values"
  sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RealToInteger block driven by the sine source"
  r2i_2 = BlockComponents.Math.RealToInteger() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c1.y, r2i.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sine.y, r2i_2.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["r2i.y", "r2i_2.y", "sine.y"]}}}
  }
}
end
```

```dyad
"""
Rounds constant and time-varying Real inputs to the nearest Integer.

Connects a constant source with value 2.7 to a RealToInteger block and verifies
that the output rounds to 3. A second RealToInteger block is driven by a sine
wave (amplitude 3) so the rounded integer output steps through several values
over time.
"""
test component RealToInteger
  "Constant source providing the Real input"
  c1 = BlockComponents.Sources.Constant(k = 2.7) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "RealToInteger block under test"
  r2i = BlockComponents.Math.RealToInteger() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 20, "x2": 270, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping negative and positive values"
  sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RealToInteger block driven by the sine source"
  r2i_2 = BlockComponents.Math.RealToInteger() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c1.y, r2i.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sine.y, r2i_2.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["r2i.y", "r2i_2.y", "sine.y"]}}}
  }
}
end
```

## Test Cases {#Test-Cases}

### Test Case `case1` {#Test-Case-case1}

```julia
plt
```

![](yprqcqr.svg)

```julia
plt
```

![](knthejn.svg)

```julia
plt
```

![](ljocgth.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`Constant`](Math.Tests.RealToInteger.md)

  * [`IntegerToBoolean`](Math.Tests.RealToInteger.md)

  * [`IntegerToReal`](Math.Tests.RealToInteger.md)

  * [`RealToInteger`](Math.Tests.RealToInteger.md)

  * [`RealToInteger`](Math.Tests.RealToInteger.md)

  * [`Sine`](Math.Tests.RealToInteger.md)
