Skip to content

Instantly share code, notes, and snippets.

View arpitbbhayani's full-sized avatar
🎲
building @DiceDB

Arpit Bhayani arpitbbhayani

🎲
building @DiceDB
View GitHub Profile
...
for (i = 0; i < size_b; ++i) {
carry += a->ob_digit[i] + b->ob_digit[i];
z->ob_digit[i] = carry & PyLong_MASK;
carry >>= PyLong_SHIFT;
}
for (; i < size_a; ++i) {
carry += a->ob_digit[i];
z->ob_digit[i] = carry & PyLong_MASK;
carry >>= PyLong_SHIFT;
struct _longobject {
PyObject ob_base;
Py_ssize_t ob_size; /* Number of items in variable part */
digit ob_digit[1];
};
typedef struct {
PyObject ob_base;
Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;
struct _longobject {
PyObject_VAR_HEAD
digit ob_digit[1];
};
>>> 2 ** 20000
39802768403379665923543072061912024537047727804924259387134 ...
...
... 6021 digits long ...
...
6309376
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%Lf\n", powl(2, 20000));
return 0;
}
$ ./a.out
inf
@arpitbbhayani
arpitbbhayani / ceval.c
Created January 2, 2020 19:31
The new Binary addition implementation
case TARGET(BINARY_ADD): {
PyObject *right = POP();
PyObject *left = TOP();
PyObject *result;
if (PyUnicode_CheckExact(left) &&
PyUnicode_CheckExact(right)) {
result = unicode_concatenate(tstate, left, right, f, next_instr);
}
else {
// Do this operation only when both the operands are numbers and
@arpitbbhayani
arpitbbhayani / ceval.c
Created January 2, 2020 19:31
Binary operate
PyObject *
binary_operate(PyObject * left, PyObject * right, char operator) {
switch (operator) {
case '+':
return PyNumber_Add(left, right);
case '-':
return PyNumber_Subtract(left, right);
case '*':
return PyNumber_Multiply(left, right);
case '/':
@arpitbbhayani
arpitbbhayani / ceval.c
Created January 2, 2020 19:30
generating random number using datetime.h in c
int
get_random_number(int max) {
return time(NULL) % max;
}
@arpitbbhayani
arpitbbhayani / ceval.c
Created January 2, 2020 19:30
Checking if both the python objects (operands) are numbers.
if (PyNumber_Check(left) && PyNumber_Check(right)) {
// Both the operands are numbers
}