如何在归一化输出中返回两个归一化值的和 [英] How to return sum of two normalized values in a normalized output

查看:98
本文介绍了如何在归一化输出中返回两个归一化值的和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,使a和b都在[0..1]范围内,并加上a和b并返回在[0..1]范围内的输出,当总和大于1时,它将换为零类似于mod函数.

I need a function that takes a and b both in [0..1] range, and adds a and b and returns the output in [0..1] range, when the sum is above 1 it wraps to zero like mod function.

我尝试了这个,但是不起作用:

I tried this but it doesn't work:

function shift(a, b) {
  return (a + b) % 1;
}

例如,对于a = 1和b = 0,它返回0,但我希望它返回1.

For example for a = 1 and b = 0, it returns 0 but I want it to return 1.

推荐答案

我认为您能做的最好的事情是:

I think the best you can do is something like:

function normalize(a, b) {
  let r = (a + b);
  return r === 1 ? 1 : r % 1;
}

当总和恰好为0时将返回0,而当总和恰好为1时将返回1.其他值换行"回到0 ... 1范围内.

This will return 0 when the sum is exactly 0, and 1 when the sum is exactly 1. Other values "wrap" back into the 0 ... 1 range.

这篇关于如何在归一化输出中返回两个归一化值的和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆