gdsfactory / kfactory

gdsfactory with a klayout backend
https://gdsfactory.github.io/kfactory/
MIT License
27 stars 10 forks source link

to_dbu fails on negative numbers #404

Open tvt173 opened 2 weeks ago

tvt173 commented 2 weeks ago

Describe the bug Calling to_dbu on postive numbers works great. Calling it on negative numbers however, produces garbage. Try this:

dbu = layout.to_dbu(-0.4)
print(dbu)

Expected result: -400 Actual result: 4294966896

It's not uncommon, for example, to pass a sizing value in microns, which might be negative. In this case, doing so causes the application to crash.

A potential workaround could be to do whatever operation is inside here on the abs() of the value passed and then multiply the final result back by the sign

sebastian-goeldi commented 2 weeks ago

Hmm, interesting. I misread the API of KLayout. The reason this happens is that the C++ API expectes/produces unsigned integers :( (and since -1 will be mapped to the biggest unsigned int64, you can imagine where the numbers will land roughly).

For now I can make a check in there and use as you said the sign separately. I can ask whether it's reasonable to modify the overloads to work on signed int/float as well.