You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel公式设计咨询:基于x、y符号差异调整5*(x-y)表达式

Excel Formula for Your Sign-Based Calculation

Got it, let's break this down step by step to build the exact formula you need. First, let's reconfirm the core logic to make sure we're aligned:

  • If x and y have opposite signs (one positive, one negative), return 5*(x-y)
  • If x and y have the same sign (both positive or both negative), return the value with the larger absolute value while keeping its original sign (e.g., x=3, y=5 → 5; x=-4, y=-2 → -4)

The Formula

Assuming your x value is in cell A1 and y is in B1, here's the ready-to-use formula:

=IF(SIGN(A1)=SIGN(B1), SIGN(A1)*MAX(ABS(A1),ABS(B1)), 5*(A1-B1))

How It Works

Let's unpack each component to understand the logic:

  1. Sign Check: SIGN(A1)=SIGN(B1)
    The SIGN function returns 1 for positive numbers, -1 for negatives, and 0 for zero. This condition instantly tells us if x and y share the same sign (or are both zero).
  2. Same Sign Case: SIGN(A1)*MAX(ABS(A1),ABS(B1))
    • ABS(A1) and ABS(B1) strip away signs to get absolute values
    • MAX(...) picks the larger of those two absolute values
    • Multiplying by SIGN(A1) re-applies the original sign (since x and y have the same sign, SIGN(B1) would work too)
  3. Opposite Sign Case: 5*(A1-B1)
    Directly applies the expression you specified for when signs don't match.

Test Examples

Let's validate with real numbers to ensure it works:

  • Opposite Signs: x=2, y=-3 → 5*(2 - (-3)) = 25 (formula returns 25)
  • Same Positive: x=4, y=7 → 1*MAX(4,7) = 7 (formula returns 7)
  • Same Negative: x=-5, y=-2 → -1*MAX(5,2) = -5 (formula returns -5)
  • Zero Edge Case: x=0, y=6 → SIGN(0)=0 vs SIGN(6)=1 (opposite signs) → returns 5*(0-6) = -30 (adjust this zero handling if needed!)

内容的提问来源于stack exchange,提问作者Stefan Iliev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:04:13