0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ラップアラウンドの関数化

Last updated at Posted at 2023-12-10

ラップアラウンドの関数化

ラップアラウンドとは、ある範囲内で値がループするような計算のことです。下限値から上限値の間で数値をループさせたいときに利用します。

コード

int Wrap(int num, int low, int high)
{
    //ゼロ除算回避
    if ((high - low) == 0)return num;

    //エラー
    if (low > high)
    {
        printf("エラー:下限値が上限値を上回っています");
        return num;
    }

    const int n = (num - low) % (high - low);
    return  (n >= 0) ? (n + min) : (n + high);
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?