21 November 2011

value of pi (π)

==> In term of fractions: 
22/7,
333/106,
355/113 = 3.1415929203539823008849557522124,
103993/33102, 
104348/33215, 
208341/66317,
312689/99532,
833719/265381,
1146408/364913,
4272943/1360120,
...
...
428224593349304/136308121570117, ...

==> From GNU's math.h header file:
# define M_PI       3.14159265358979323846  /* pi */

Reference:
http://numbers.computation.free.fr/Constants/Pi/piApprox.html
http://c2.com/cgi/wiki?ValueOfPi

10 November 2011

htonll - 64bit host to network conversion

See difference in output for little endian and big endian machines (bswap64.c)

Constant folding is the process of simplifying constant expressions at compile time.
With constant folding support, i = 1 * 2 * 3 * 4 * 5; is calculated at compile time, i = 120.
#include <endian.h>

typedef unsigned long long uint64;
typedef unsigned long uint32;

typedef union {
  uint64 big;
  uint32 small[2];
}long_long;

#define HTONLL01(x) ({long_long i; i.small[0] = htonl((uint32)(x>>32)); \
                      i.small[1] = htonl((uint32)x); x = i.big;})

#if __BYTE_ORDER == __BIG_ENDIAN
# define HTONLL02(x) (x)
#else
# if __BYTE_ORDER == __LITTLE_ENDIAN
#  define HTONLL02(x) (((uint64)htonl((uint32)x))<<32 | htonl((uint32)(x>>32)))
# endif
#endif

From "/usr/include/bits/byteswap.h"
/* Swap bytes in 64 bit value.  */
#define __bswap_constant_64(x) \
     (  (((x) & 0xff00000000000000ull) >> 56)                     \
      | (((x) & 0x00ff000000000000ull) >> 40)                     \
      | (((x) & 0x0000ff0000000000ull) >> 24)                     \
      | (((x) & 0x000000ff00000000ull) >> 8)                      \
      | (((x) & 0x00000000ff000000ull) << 8)                      \
      | (((x) & 0x0000000000ff0000ull) << 24)                     \
      | (((x) & 0x000000000000ff00ull) << 40)                     \
      | (((x) & 0x00000000000000ffull) << 56))

# define __bswap_64(x) \
     (__extension__                                               \
      ({ union { __extension__ unsigned long long int __ll;       \
         unsigned long int __l[2]; } __w, __r;                    \
     if (__builtin_constant_p (x))                                \
       __r.__ll = __bswap_constant_64 (x);                        \
     else                                                         \
       {                                                          \
         __w.__ll = (x);                                          \
         __r.__l[0] = __bswap_32 (__w.__l[1]);                    \
         __r.__l[1] = __bswap_32 (__w.__l[0]);                    \
       }                                                          \
     __r.__ll; }))

Reference:
http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/
http://en.wikipedia.org/wiki/Constant_folding

09 November 2011

ഒരു ട്രെയിന്‍ യാത്ര

On a fine Sunday evening same 5pm train.
I have got side lower seat.

അടുത്തിരുന്ന couples പരിചയപ്പെട്ടു, about 60+ years old.
Later came to know that they where teachers and taught at my sis’s school and seemed neighbors to me, may be within 5km radius. And they are going to see some swami in Bangalore…blah blah blah... And the husband had heart attack once,,, :-(

And I started,,, reading + listening to music.
From trissur (or some where) 1 more couple came,,, seemed they got both the lower berths.
And they switched of the light immediately after finishing the dinner.

I don’t know why I interfere with the matter, may be I don’t want the lights gone at that moment. I said them that old couple can’t climb the berth.
I volunteered to give my side lower berth, so as the new couple also agreed to give one of there lower berth.
Here is the catch,,, I didn’t check their tickets.

So at some point of the journey,,, some where in തമിഴ്നാടു കുറെ ആളുകള്‍ ട്രെയിനില്‍ കയറി….
They started saying that i am sleeping on their berth and one of the younger couple as well...
As all of us was on sound sleep,,, we didn’t understand a thing for a moment.

കുറച്ചു സമയം കഴിഞ്ചു നമ്മുടെ old couplinte ടിക്കറ്റ്‌ ചെക്ക്‌ ചെയ്തപ്പോഴാണ് ഒരു കാര്യം മനസിലായത്... അവരുടെ berth S1ല്‍ അന്നു instead of S10…

പാവം ഞാന്‍... New couplinte വഴക്കും കേട്ടു,,, ഞാന്‍ പറഞ്ഞിട്ടാണ്...

പിന്നെ രാത്രി 2pm ന് നമ്മുടെ old couplinem അയിട്ടൂ S10ന്നു S1 വരെ നടന്നു...
അവിടെ ചെന്നപ്പോള്‍ ഭാഗ്യത്തിന് bearth അവിടെ തന്നെ ഉണ്ട്...

പക്ഷെ ഒരു problem... നമ്മുടെ മാഷ് കണ്ണാട എടുക്കാന്‍ മറന്നു...
I came back from S1 to S10, took the കണ്ണാട, went back to S1 with കണ്ണാട
അവിടെ എത്തി അപ്പോഴത്തെ കാഴ്ച്ച... നമ്മുടെ heart patient അയ മാഷ് side upper beartil കയറി കാലും അട്ടി കൊണ്ടിരിക്കുന്നു...

Gheee...

Does this need any moral???