Stream: helpdesk (published)

Topic: ✔ negation on integer


view this post on Zulip ShalokShalom (Sep 27 2023 at 19:46):

I may miss the obvious, but why is there no method assigned to the negation operator?

I want to do the following:

function check(number)
    if ! number >= 0
        error("number must be non-negative")
    end
end

view this post on Zulip Sebastian Pfitzner (Sep 27 2023 at 19:51):

not sure what exactly you're asking. There certainly are methods assigned to it:

julia> methods(!)
# 4 methods for generic function "!" from Base:
 [1] !(f::ComposedFunction{typeof(!)})
     @ operators.jl:1089
 [2] !(f::Function)
     @ operators.jl:1088
 [3] !(x::Bool)
     @ bool.jl:35
 [4] !(::Missing)
     @ missing.jl:101

Your example just needs parentheses

view this post on Zulip ShalokShalom (Sep 27 2023 at 19:55):

Well, not to an integer. I already tried to put parens around it, but that didnt help. I tried now once again, and obviously, it works :P
I may have had the old state saved in the REPL, now it works. :D

view this post on Zulip Notification Bot (Sep 27 2023 at 19:56):

ShalokShalom has marked this topic as resolved.

view this post on Zulip Mason Protter (Sep 27 2023 at 21:29):

I do sometimes feel that python's not xis more legible sometimes than our !x. I guess I could always just do

macro not(x)
    Expr(:call, !, esc(x))
end

view this post on Zulip jar (Sep 27 2023 at 21:50):

I tend to think unary operators are just not worth it

view this post on Zulip Mason Protter (Sep 27 2023 at 21:51):

I'm very very thankful we have ' for adjoint.

view this post on Zulip Mason Protter (Sep 27 2023 at 21:52):

I kinda wish we had more postfix unary operators

view this post on Zulip jar (Sep 27 2023 at 21:54):

Yeah I just meant the prefix ones. I even have some postfixes in mind.

view this post on Zulip Sundar R (Sep 28 2023 at 06:10):

Mason Protter said:

I do sometimes feel that python's not xis more legible sometimes than our !x. I guess I could always just do

Someone at jolin.io seems to have had similar thoughts: https://github.com/jolin-io/NotMacro.jl/blob/main/src/NotMacro.jl

view this post on Zulip ShalokShalom (Sep 28 2023 at 06:28):

Yes, this is why I opened https://github.com/JuliaLang/julia/issues/51485

You might help with emojicons and arguments :D

view this post on Zulip ShalokShalom (Sep 28 2023 at 06:33):

Mason Protter said:

I do sometimes feel that python's not xis more legible sometimes than our !x.

Names are always understood by anyone. Show me anyone, who doesn't know what not means. On the other hand, no one, who hasn't programmed yet already known that ! is supposed to has the meaning of no

So, particularly to introduce new people to programming, this seems to show, that Julia is currently not trying its best to onboard them.

view this post on Zulip Mason Protter (Sep 28 2023 at 10:05):

Jar's point isn't really about not vs !. It's more just that not x isn't much of a win vs not(x).

view this post on Zulip Mason Protter (Sep 28 2023 at 10:06):

Whining that any syntax you don't like is julia "not trying its best to onboard them" is not really productive or helpful

view this post on Zulip Sascha Mann (Sep 28 2023 at 10:18):

Sundar R said:

Mason Protter said:

I do sometimes feel that python's not xis more legible sometimes than our !x. I guess I could always just do

Someone at jolin.io seems to have had similar thoughts: https://github.com/jolin-io/NotMacro.jl/blob/main/src/NotMacro.jl

# macroexpand solves `@not @not true && false` case

I'm not even sure what I'd want that to mean. That's a case where ! seems much clearer

view this post on Zulip Mason Protter (Sep 28 2023 at 10:24):

yeah, that seems totally unnecessary and not such a good idea

view this post on Zulip Mason Protter (Sep 28 2023 at 10:31):

They likely thought there was a performance advantage to it because the code_llvm for @not @not x && y looks shorter than the code_llvm for !(!(x && y)), but at least on x86, they still compile to identical code_native.

view this post on Zulip Mason Protter (Sep 28 2023 at 10:52):

julia> using NotMacro

julia> code_native(Tuple{Bool, Bool}) do x, y
           @not @not x && y
       end

        .text
        .file   "#1"
        .globl  "julia_#1_750"                  # -- Begin function julia_#1_750
        .p2align        4, 0x90
        .type   "julia_#1_750",@function
"julia_#1_750":                         # @"julia_#1_750"
; ┌ @ REPL[13]:5 within `#1`
        .cfi_startproc
# %bb.0:                                # %top
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, %eax
; │┌ @ bool.jl:35 within `!`
        andb    $1, %al
        negb    %al
        andb    %sil, %al
; │└
                                        # kill: def $al killed $al killed $eax
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq
.Lfunc_end0:
        .size   "julia_#1_750", .Lfunc_end0-"julia_#1_750"
        .cfi_endproc
; └
                                        # -- End function
        .section        ".note.GNU-stack","",@progbits

view this post on Zulip Mason Protter (Sep 28 2023 at 10:53):

julia> code_native(Tuple{Bool, Bool}) do x, y
           !(!(x && y))
       end

        .text
        .file   "#1"
        .globl  "julia_#1_752"                  # -- Begin function julia_#1_752
        .p2align        4, 0x90
        .type   "julia_#1_752",@function
"julia_#1_752":                         # @"julia_#1_752"
; ┌ @ REPL[14]:4 within `#1`
        .cfi_startproc
# %bb.0:                                # %top
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, %eax
; │┌ @ bool.jl:35 within `!`
        andl    %esi, %eax
; │└
        andb    $1, %al
                                        # kill: def $al killed $al killed $eax
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq
.Lfunc_end0:
        .size   "julia_#1_752", .Lfunc_end0-"julia_#1_752"
        .cfi_endproc
; └
                                        # -- End function
        .section        ".note.GNU-stack","",@progbits
nothing
Main.B

view this post on Zulip ShalokShalom (Sep 28 2023 at 11:38):

Mason Protter said:

Whining that any syntax you don't like is julia "not trying its best to onboard them" is not really productive or helpful

I did indeed mean not vs ! and I gave reasoning, why I think one is more approachable.

view this post on Zulip ShalokShalom (Sep 28 2023 at 11:42):

I indeed quoted the wrong person, and meant my statement towards you (and simply agreed with you) :big_smile:


Last updated: Nov 22 2024 at 04:41 UTC