推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
noahhhh
V2EX  ›  Python

Python 怎么提取正负号

  •  
  •   noahhhh · Sep 24, 2022 via Android · 5776 views
    This topic created in 1501 days ago, the information mentioned may be changed or developed.
    我想用零次方取正负,比如 -3**0=-1 。但是用 n=-3 n**0=1 ,这是什么原因。
    15 replies  •  2022-09-26 10:19:18 +08:00
    deplivesb
    deplivesb
    Sep 24, 2022
    1
    任何数的 0 次方等于几?数学上咋学的?
    yanqiyu
    yanqiyu
    Sep 24, 2022
    2
    就这么说罢,a^0 * a = a^1 按照定义始终成立
    a^1 = a 按照定义始终成立
    这时候你猜猜良定义的 a^0 是?
    noahhhh
    noahhhh
    OP
    Sep 24, 2022 via Android
    3
    @deplivesb 🙈完了,白学了
    @yanqiyu
    shushiyuedu
    shushiyuedu
    Sep 24, 2022
    4
    n=-3 # n 是要提取符号的数
    x = '+' if n>0 else '-' # x 是提取结果
    shushiyuedu
    shushiyuedu
    Sep 24, 2022
    5
    @noahhhh 你自己改吧,你自己没定义 0 对应啥符号,哈哈
    datou
    datou
    Sep 24, 2022
    6
    非零实数的零次幂都等于 1 吧?
    krixaar
    krixaar
    Sep 24, 2022❤️1
    7
    直接 n/abs(n)然后 0 作为特例呗
    aloxaf
    aloxaf
    Sep 24, 2022
    8
    用 math.copysign
    imkuang
    imkuang
    Sep 24, 2022 via Android❤️10
    9
    直接判断大于 0 小于 0 不就行了吗
    HFcbyqP0iVO5KM05
    HFcbyqP0iVO5KM05
    Sep 24, 2022 via iPhone
    10
    同楼上,到底是什么原因不能判断是否小于零? enlight me please
    jinliangtux
    jinliangtux
    Sep 24, 2022
    11
    (-3)**0 = 1
    xhuuanniqege
    xhuuanniqege
    Sep 24, 2022 via Android
    12
    一个是-(3)^0 ,一个是(-3)^0 ,有啥好奇怪的吗
    tf2
    tf2
    Sep 24, 2022
    13
    数学能力点过头了,工程能力欠缺。

    python2: cmp()
    python3: (a > b) - (a < b)
    mikewang
    mikewang
    Sep 25, 2022❤️1
    14
    简单的做法:(1, -1)[n < 0],如果需要 0 值也返回 1 的话。
    fkdtz
    fkdtz
    Sep 26, 2022
    15
    不吃从何笑起
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4848 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 01:16 · PVG 09:16 · LAX 17:16 · JFK 20:16
    ♥ Do have faith in what you're doing.