Skip to content

Casting from f64 to u64 does not work #1867

Closed

Description

@ronnuriel

Fix error when converting between two different types
for example:
x:i16 = i16(3)
y:u16 = x

Converting i16 to u16.

Activity

  1. added
    cLabel for C language related changes
    high priorityThe issue is blocking other projects, not easy to workaround, must be fixed as soon as possible
    on May 29, 2023
  2. self-assigned this
    on May 29, 2023
  3. Smit-create commented on May 29, 2023

    @Smit-create
    Collaborator

    You need to explicitly define the cast. The following should work fine:

    def f():
        x:i16 = i16(3)
        y:u16 = u16(x)
        print(x, y)
    
    f()
  4. removed their assignment
    on May 29, 2023
  5. certik commented on May 29, 2023

    @certik
    Contributor

    Thanks @Smit-create, indeed the code you posted works for me in both LLVM and C backends:

    $ lpython a.py 
    3 3
    $ lpython --backend=c a.py
    3 3

    @ronnuriel can you please try it out and let us know if that fixes it for you?

  6. ronnuriel commented on Jun 4, 2023

    @ronnuriel
    CollaboratorAuthor

    The following works:
    cycles_count: i64
    end_cycle: i64 = i64(100)
    start_cycle: i64 = i64(4)
    num_iters: i64 = i64(4)

    cycles_count = i64((end_cycle - start_cycle) / num_iters)
    

    but when u64 its not:
    cycles_count: u64 #THIS LINE
    end_cycle: i64 = i64(100)
    start_cycle: i64 = i64(4)
    num_iters: i64 = i64(4)

    cycles_count = u64((end_cycle - start_cycle) / num_iters)
    

    ERROR:
    |
    181 | cycles_count = u64((end_cycle - start_cycle) / num_iters)
    | ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch ('u64' and 'f64')

  7. certik commented on Jun 4, 2023

    @certik
    Contributor

    I see, casting from f64 to u64 does not work. I think that's a bug, thanks for reporting it. As a workaround until this gets fixed, this worked for me:

        cycles_count = u64(i64((end_cycle - start_cycle) / num_iters))
    
  8. changed the title [-]converting between different types - C backend [/-] [+]Casting from f64 to u64 does not work[/+] on Jun 4, 2023
  9. self-assigned this
    on Jun 5, 2023
  10. Darshan4852 commented on Jun 9, 2023

    @Darshan4852

    x: i16 = i16(3)
    y: u16

    if x >= 0:
    y = u16(x)
    else:
    y = u16(2 ** 16 - 1)

    I hope it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

cLabel for C language related changeshigh priorityThe issue is blocking other projects, not easy to workaround, must be fixed as soon as possible

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions