修改浮标!


19

3D建模软件主要使用UV贴图将纹理映射到3D对象上。U和V的有效值通常位于一个包含[0..1]范围内。

挑战

您购买了一个超级易于使用的新3D建模软件。但是,它有一个问题:它会从UV值中添加或减去一个随机整数。您的任务是创建一个程序或函数,以修改输入值以获取包含[0..1]范围内的浮点值。

生成的浮点数应与原始浮点数相同,并尽可能接近原始浮点。因为01都在输出范围内,所以0或更小的整数应更改为0,1或更大的整数应更改为1。

JavaScript中的示例算法:

function modFloat(input) {
    while (input < 0 || input > 1) {
        if (input < 0) input += 1;
        if (input > 1) input -= 1;
    }
    return input;
}

规则

  • 输入是单个整数或浮点值。只要您的答案中指定了任何合理的格式。
  • 输出应为浮点值的十进制表示。
  • 输出精度应至少等于输入的小数位。
  • 允许尾随零。
  • 确保您的代码正确选择要为整数输入输出的0或1。

测试用例

Input       | Output
------------+---------
         -4 | 0
         -1 | 0
          0 | 0
          1 | 1
          2 | 1
     1.0001 | 0.000100
 678.123456 | 0.123456
-678.123456 | 0.876544
        4.5 | 0.5

这是,因此以字节为单位的最短代码胜出!


4
您打算将1映射到1吗?通常使用半开范围。根据您的伪代码,我是否应该理解所有> 1都为1的整数,所有<0都为0的整数>
xnor

4
如果输入为1,则所有%1解决方案都将失败!
seshoumara

9
实际上,我喜欢1-> 1的东西,这使问题不会成为许多语言的琐碎内置函数。
xnor

2
我可以使用sed吗?sed中没有数据类型,输入必须是文本流。
seshoumara

1
@seshoumara any reasonable input format is allowed,所以我会说“为什么不呢?”。
lolbas '17

Answers:


1

果冻,6个字节

%1o>0$

在线尝试!

果冻没有TrueFalse,但是使用10代替它们。

%1o>0$ - Main link: float v
%1     - v mod 1
     $ - last two links as a monad
   >0  -     v greater than zero?
  o    - or - replace the 0 result of the mod with 1 when v is greater than 0.


6

Brachylog14 11字节

感谢Fatalize打高尔夫球3个字节。

∧≜:?+.≥0∧1≥

对于更改,此答案不使用mod :)

在线尝试!

说明

∧≜                Label an integer variable. This will start trying different
                  values for this variable, the ones closest to 0 first.
   :?+.           This variable summed to the input is equal to the output
      .≥0∧1≥      which is >= 0 and <= 1

当我在线尝试时,此输出为正整数输出0。
尼尔

1
@Neil更正了,谢谢。我不知道为什么我错过了它
Leo

2
您可以这样保存3个字节:∧≜:?+.≥0∧1≥
Fatalize

4

JavaScript(ES6),19个字节

n=>(n%1+1)%1||n>0|0

在JavaScript中,n%x如果n为负,则返回一个负数,这意味着如果要获得正的残基,则必须添加x如果n为负。(n%x+x)%x涵盖所有情况:

n     n%1   n%1+1 (n%1+1)%1
0     0     1     0
1     0     1     0
2.4   0.4   1.4   0.4
-1    0     1     0
-2.4  -0.4  0.6   0.6

另一个有效的解决方案(20字节),显示了更多模式:

n=>n%1+(n%1?n<0:n>0)

3

MATL,9个字节

1&\0>yg>+

在线尝试!验证所有测试用例

说明

输入示例 678.123456

1      % Push 1
       % STACK: 1
&\     % Implicit input. Divmod with 1
       % STACK: 0.123456, 678
0>     % Is it positive?
       % STACK: 0.123456, 1
y      % Duplicate from below
       % STACK: 0.123456, 1, 0.123456
g      % Convert to logical: nonzero becomes 1
       % STACK: 0.123456, 1, 1
>      % Greater than? This is true if fractional part of input was zero
       % and non-fractional part was positive
       % STACK: 0.123456, 0
+      % Add. Implicitly display
       % STACK: 0.123456

3

Javascript,28个字节

m=f=>f<0?m(f+1):f>1?m(f-1):f

递归地将值减小/增加1,直到结果为[0,1]


欢迎使用PPCG,并提供不错的答案!
ETHproductions'2

2

Japt,8字节

u1 ªUbV1

在线测试!

我认为这是我第一次使用b...

说明

 u1 ªUbV1  // Implicit: U = input, V = 0
Uu1        // Take U%1, but add 1 if U is negative. This is equivalent to %1 in Python.
    ª      // If the result is falsy (0), instead take
     UbV1  //   U bound between 0 and 1.
           // This converts positive integers to 1, zero/negative integers to 0.
           // Implicit: output result of last expression

2

Mathematica,20个字节

#~Mod~1/. 0/;#>0->1&

说明

这是/;我使用它的地方的一种非常不寻常的用法,&&因为它之后的条件与其匹配的模式无关。

#~Mod~1...

Compute x % 1,对所有情况(正整数除外)都是正确的。

.../. 0/;...

如果...,在上一个表达式中替换零。

...#>0...

输入为正

...->1...

1


2

PHP,37字节

<?=($m=fmod($argn,1))+(!!$m^$argn>0);

用运行echo <number> | php -R '<code>'

有很多方法可以做到这一点……这应该是PHP中最短的方法之一。

fmod结果是负的花车和负0为正整数; 需要调整的参数:!!$m对于浮点数为true,异或$n>0结果为正浮点数和负整数为false,为负浮点数和正整数为true;+铸件,要10-完成。


2

C 57 56 73字节

b;f(float n){b=n;printf("%f",((!(n-b)&&n<=0)?0:n<0?1.+n-b:(n-b)?n-b:1));}

@ pinkfloydx33感谢您指出!

非高尔夫版本:

f(float n)
{
  int b=n;
  printf("%f",( (!(n-b)&&n<=0)?0:n<0?1.+n-b:(n-b)?n-b:1) );
}

在线尝试!


你能1.代替1.0吗?
Kritixi Lithos

@KritixiLithos我不熟悉这种表示法,但它似乎可以工作。
亚伯汤姆

您必须叫所有东西f吗?😂另外,我也不认为括号内(f<0)是必需的。
kennytm '17

我认为这可以简化,而不是重复减法。但是,无论哪种方式都不起作用f(1)(应该返回1)
pinkfloydx33

@ pinkfloydx33非常感谢您指出它,该代码还远远没有完成。:)修复了它,现在应该可以正常运行!
亚伯汤姆

1

SmileBASIC,28个字节

INPUT N?N-FLOOR(N)+(N<<0==N)

1

JavaScript(ES6),19个字节

n=>(n>0==!(n%=1))+n

说明: %1在所有情况下都不能给出正确的结果:

input       %1          output
-ve int     -0
-ve frac    -ve frac    +ve frac
0           0
+ve frac    +ve frac
+ve int     0           1

在错误的情况下,需要添加一个额外的1,即负非整数和正整数的情况。这就是表达式的(n>0==!(n%1))计算结果。


还有其他一些安排,但是我还没有找到一个更短的安排……
ETHproductions

1

> <>,26个字节

:1%:?vr1(?v1n;
     >n;n0<

在线尝试!

因为解决方案很好几乎总是立即给出使用高尔夫语言的,所以我决定将它们混为一谈。首先<> <回答!

说明

:1%:?vr1(?v1n;    Assume input i in stack
     >n;n0<

:                 Duplicate i (need it if i%1 != 0)
 1                Push 1
  %               Pop i and 1, push i%1
   :              Duplicate top of stack because  we need one for the if     
    ?v            If i%1 != 0 ------------------------,
      r           Reverse stack so that i is TOS      | 
       1(?v       If i > 0 (not < 1)                  |
           1n;      Print 1 and Exit                  |
                  Else                                |                   
        n0<         Print 0 and --,                   |
     >n           Print n <-------|-------------------'
       ;          Exit <----------'

有趣的事实:解释是有效的<> <程序!



0

Pyth,7个字节

|%Q1s<0

说明

|%Q1s<0
|%Q1s<0Q      Implicitly add input
 %Q1          Input mod 1
|             Short-circuting or
    s<0Q      1 if input is positive, 0 otherwise

如果您不介意使用True和False作为1和0,则可以删除s6个字节。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.