Skip to content

Casting from f64 to u64 does not work #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #1802
ronnuriel opened this issue May 29, 2023 · 5 comments · Fixed by #1889
Closed
Tracked by #1802

Casting from f64 to u64 does not work #1867

ronnuriel opened this issue May 29, 2023 · 5 comments · Fixed by #1889
Assignees
Labels
c Label for C language related changes high priority The issue is blocking other projects, not easy to workaround, must be fixed as soon as possible

Comments

@ronnuriel
Copy link
Collaborator

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

Converting i16 to u16.

@ronnuriel ronnuriel added c Label for C language related changes high priority The issue is blocking other projects, not easy to workaround, must be fixed as soon as possible labels May 29, 2023
@Smit-create Smit-create self-assigned this May 29, 2023
@Smit-create
Copy link
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()

@Smit-create Smit-create removed their assignment May 29, 2023
@certik
Copy link
Contributor

certik commented May 29, 2023

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?

@ronnuriel
Copy link
Collaborator Author

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')

@certik
Copy link
Contributor

certik commented Jun 4, 2023

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))

@certik certik changed the title converting between different types - C backend Casting from f64 to u64 does not work Jun 4, 2023
@certik certik mentioned this issue Jun 4, 2023
25 tasks
@Smit-create Smit-create self-assigned this Jun 5, 2023
@Darshan4852
Copy link

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
Labels
c Label for C language related changes high priority The issue is blocking other projects, not easy to workaround, must be fixed as soon as possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants