Python Forum
What is the equivalent python code for c program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the equivalent python code for c program?
#1
What is the Python Equivalent code for C Program:

int main()
{
buildUp();
return 0;
}

void buildUp(void)
{
unsigned long d;
unsigned index;
outbox[0] = 0;
outbox[1] = 0;
outbox[2] = 64;
outbox[3] = 65;

d = ((long)outbox[3] << 24);
d|= ((long)outbox[2] << 16);
d|= ((long)outbox[1] << 8);
d|= ((long)outbox[0]);


float member = *(float *)&d;

printf("Float output=%f\n\n",member);

return member;
}
Reply
#2
def build_up():
    outbox = [0, 0, 64, 65]

    d = outbox[3] << 24 | outbox[2] << 16 | outbox[1] << 8 | outbox[0]
    member = float(d)

    print(f'Float output = {member}')

    return member

def main():
    build_up()

if __name__ == '__main__':
    main()
Output:
Float output = 1094713344.0
Reply
#3
Don't understand why you need |. If numbers are not greater then 255.
member = float(sum([i << s for i, s in zip([0,0,64,65], [0, 8, 16, 24])]))
print(member)
Output:
1094713344.0

Quote:float member = *(float *)&d;
Numpy might have a better solution then this.
import numpy as np

array = np.array((0,0,64,65), np.int32) << np.array((0, 8, 16, 24), np.int32)
d = np.sum(array)
member = np.array((d, 0))
member.dtype = np.float32
print(member[0])

Output:
#include <stdio.h> int main() { unsigned long d = (64 << 16) + (65 << 24); float member = *(float *)&d; printf("Float output=%f\n\n", member); // 12.0 printf("%lu", d); // 1094713344.0 return 0; }
99 percent of computer problems exists between chair and keyboard.
Reply
#4
Time of execution:
import time
import numpy as np


def build_up():
    outbox = [0, 0, 64, 65]

    d = outbox[3] << 24 | outbox[2] << 16 | outbox[1] << 8 | outbox[0]
    member = float(d)

    print(f'Float output = {member}')

    return member

def build_up1():
    member = float(sum([i << s for i, s in zip([0, 0, 64, 65], [0, 8, 16, 24])]))
    print(member)

def build_up2():
    array = np.array((0, 0, 64, 65), np.int32) << np.array((0, 8, 16, 24), np.int32)
    d = np.sum(array)
    member = np.array((d, 0))
    member.dtype = np.float32
    print(member[0])

def main():
    t0 = time.clock()
    build_up()
    t1 = time.clock()
    print(f'time build_up: {t1-t0}')

    t0 = time.clock()
    build_up1()
    t1 = time.clock()
    print(f'time build_up1: {t1-t0}')


    t0 = time.clock()
    build_up2()
    t1 = time.clock()
    print(f'time build_up2: {t1-t0}')

if __name__ == '__main__':
    main()
Output:
Float output = 1094713344.0 time build_up: 1.8943810864992326e-05 1094713344.0 time build_up1: 6.911930991280987e-06 12.0 time build_up2: 6.348736614213645e-05
Build_up1 wins
Reply
#5
Larz60 is really an old and learned man(aetate et sapientia). Long life to him.
Reply
#6
For me, the equivalent python code is
import struct
def buildUp():
    return struct.unpack('>f', struct.pack('>BBBB', 65, 64, 0, 0))[0]
Note that your C code doesn't compile or run. It would be a good idea to post working code!
Reply
#7
NumPy option.
import numpy as np

array = np.array((0, 0, 64, 65)) << np.array((0, 8, 16, 24))
# have to change type form np.int64 to np.int32. To get np.float32.
d = np.sum(array).astype(np.int32)
print(d.view(np.float32))

Might be a little better.
import numpy as np

array = np.array((0, 0, 64, 65)) << np.array((0, 8, 16, 24))
d = np.sum(array, dtype=np.int32).view(np.float32)
print(d)
99 percent of computer problems exists between chair and keyboard.
Reply
#8
Doing this test with ramp up time, and adding return statement to
all, as build_up 1 had, a print statement, and 1000 iterations:

build_up was faster than build_up1 by a factor of 2
And build_up 3 disqualifies as it returns wrong answer.

import time
import numpy as np


def build_up():
    outbox = [0, 0, 64, 65]

    d = outbox[3] << 24 | outbox[2] << 16 | outbox[1] << 8 | outbox[0]
    member = float(d)
    return member

def build_up1():
    member = float(sum([i << s for i, s in zip([0, 0, 64, 65], [0, 8, 16, 24])]))
    return member

def build_up2():
    array = np.array((0, 0, 64, 65), np.int32) << np.array((0, 8, 16, 24), np.int32)
    d = np.sum(array)
    member = np.array((d, 0))
    member.dtype = np.float32
    return member


def main():
    #Throw on away, statr up overhead
    member = build_up()

    t0 = time.clock()
    for x in range(1000):
        member = build_up()
    t1 = time.clock()
    print(f'Float output = {member}')
    print(f'time build_up: {(t1-t0)/1000}')


    t0 = time.clock()
    for x in range(1000):
        member = build_up1()
    t1 = time.clock()
    print(f'Float output = {member}')
    print(f'time build_up1: {(t1-t0)/1000}')


    t0 = time.clock()
    for x in range(1000):
        member = build_up2()
    t1 = time.clock()
    print(f'Float output = {member}')
    print(f'time build_up2: {(t1-t0)/1000}')

if __name__ == '__main__':
    main()
Output:
Float output = 1094713344.0 time build_up: 4.671965315329499e-07 Float output = 1094713344.0 time build_up1: 9.267131200817964e-07 Float output = [ 12. 0.] time build_up2: 4.925915430003848e-06
Reply
#9
@Larz60 12.0 is the right answer because of c code float member = *(float *)&d;
99 percent of computer problems exists between chair and keyboard.
Reply
#10
@Larz60+ The struct method is faster and it is included in the standard library.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python equivalent of Matlab code kwokmaster 5 6,999 Apr-03-2020, 12:02 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020