Is it possible to compile the Nim compiler to Javascript? That would make it
possible to compile Nim programs entirely in the browser, without any server
side steps, which I want to do.
the reason I thought this way, was because I translated the Nim code in my head
to C, to something like that:
void flt2arr(double x, uint8_t[] result) {
for(int i = 0; i < 8; i++)
result[i] = (uint8_t*)&x + i;
}
Run
But as @araq said, this is not wha
Sorry, I don’t see any aliasing in the code presented by `e`. The float value
is copied into an array of uint8 and there is no sharing of memory. It would be
different is one had used pointers, of course.
But, with unions, you have an aliasing as two fields refer to the same address
in the unio
Does it also use an union when compiling to C++?
Nim compiles the `cast` to the C union variant. (Well it should, if not, file a
bug report.)
In C99 (ISO/IEC 9899:1999 6.5/7; the exact same wording is used in ISO/IEC
9899:2011 §6.5/7)
> An object shall have its stored value accessed only by an lvalue expression
> that has one of the following types ... a character type.
The meaning of "a character type" here includes unsigned char, w
The following should also be safe, this translates to a memcpy() at the C
level. This may seem like overkill, but the C compiler should recognize the use
of memcpy for type punning and optimize it away and generate a register to
register move.
proc flt2arr(f: var float64, a: var ar
afaik atleast in C there's no "safe" way to do it. The method presented by @e
works in most situations, but breaks strict aliasing (a poiner of one type may
not be casted to a pointer of an incompatible type).
The usual safer way, in C is by using a union:
type
Uint8ToFloa
Thank you very much, this is exactly what I need. I was thinking about using
casts but wasn't really sure if it's safe.
Here's a little program demonstrating conversion functions:
import parseutils, os
func flt2arr (x: float64) : array[0..7, uint8] =
result = cast[array[0..7, uint8]](x)
func arr2flt (a: array[0..7, uint8]) : float64 =
result = cast[float64](a)
Hello,
I was wondering if it's possible to convert a `float64` into 8 `uint8` s. To be
more precise, I need to add the `float64` into a `seq[uint8]`, and then have a
way of converting the corresponding bytes back to a `float64`.
Can this be achieved? If so, how?
`--app:lib` without `-d:useNimRtl` should do the job but note that it is not
covered by tests. PRs are welcome.
I stand corrected, my Python is too rusty. :-)
No, without `global` it works and even this way:
def f():
print(x)
x = 4
f()
Run
But, the following doesn’t work and you have indeed to specify `global` (quite
different from Nim here as the “equivalent” would be `var x = 2 * x + 1` which
compiles
It doesn't even work in Python properly, ymmv:
x = 4
def f():
# need to tell Python here what it should already know:
global x
print x
Run
15 matches
Mail list logo