Recently in Programming Category

float,int类型转换

| 5 Comments | No TrackBacks
float与int之间类型转换的低效是由其bit存储结构决定的(见图)。例如:
int i = 5;
float f = i;
其中i与f的二进制存储结构是不同的,二者的转化涉及到了存储结构的转换。
对于i来说,
5 => 5 * 2^0
5 => 2.5 * 2^1
5 => 1.25 * 2^2
因此当i向f进行转换时,需要计算小数部分23bits中每一位的值,使其无限接近0.25。后再计算exp部分使exp-127的值为2。从而得到最终的f。
另外一个例子:
int i = 5;
float f = *(*float)&i;
在这个例子中,由于i的存储结构并不会发生变化,也就是
00000000 00000000 00000000 00000101
但是它会被按照float的存储方式来解释,也就是
1.(2^(-21)+2^(-23)) * 2^(0-127)
因此最终f的值将会非常非常的小。
bit

Scalability

| No Comments | No TrackBacks
在做research project和写paper的时候,有一个问题一直困扰着我。究竟系统或算法的input size (e.g. dataset的大小) 增加到多少才算进行了一次有效的scalability test?毕竟很多系统都是在input增大到一定程度的时候才会出现问题,但是这个程度却似乎很难估计。甚至做实验的时候,有时很难把这个input size增加到很大,倒不是不想,而是有很多东西限制着。记得前一阵在做XML keyword search system,我们首先需要parse XML document并且为其中每一个keyword通过B+ tree建立inverted list,但是当XML document增大到上百兆的时候,建立index的过程将是痛苦的,在laptop上运行了1个多小时,index文件增加到上G,却依然没有结束,无奈之下,在最后的scalability test中放弃了选择如此大的input size,太耗费时间了,但如果input size达不到一定值得话似乎又说明不了什么问题。或许给我一台64位高配置的机子情况会有所不同。
最近看了一篇文章,google也会有scalability的问题,这要得从gBrain extension for Firefox说起,感兴趣的可以看这篇文章Google Public Relations,这里真得赞一下google对待其用户的态度。

郁闷的indentation

| 2 Comments | No TrackBacks
一直都想把web programming好好学习和实践一下,可惜总是持续了一段时间就停下了。最近由于xml keyword search research的一些idea用web的方式能够更好的呈现,因此又重新啃起了python。把lighttpd server配置好,随便写了一个小程序想试着运行一下,结果浏览器上出现了internal server error。对着程序检查了好久也没有发现什么问题。写程序用的是Notepad++,最后实在没辙只好打开IDLE,想用python shell run一下。打开一看就傻眼了,其中有一行代码因为偷懒是从另外一个地方copy过来的,所以indentation没有consistent,有一行代码用了4个空格,其他的用的是tab,在Notepad++中也没有看出来,就没有朝indentation去想。编程有时真是一个silly mistake就可以浪费很长的时间。

创建文件时小心文件名

| No Comments | No TrackBacks
前一阵在做一个关于xml的research project的prototype,在这个项目中需要用fopen为每一个tag建立一个同名文件来保存相关的position information。结果发现在创建一个名为aux的文件时返回NULL指针,后来查阅资料原来con,aux,prn是系统保留关键字,是DOS的系统device句柄,con是标准输入输出,aux是com1,prn是printer接口。因此在创建以这三个关键字命名的文件时都会返回NULL指针,而且在创建以con.,aux.,prn. 为前缀的文件时也会返回空指针,例如创建aux.a文件,prn.xn文件等等。

C++一些容易被忽视的points

| 2 Comments | No TrackBacks
今天又拿出尘封已久的BJ的The C++ Programming Language翻了翻,发现了一些容易被忽视、遗忘但有很重要的细节,记录在这里算是一个备忘吧。
  1. pointerbool类型之间存在implicit conversion。bool至少和char占用空间相同。
  2. 一个char 8位字符的value是从0到255还是从-127到+127是implementation-defined。但从0到127这部分是相同的。为了保证可移植性,赋值时尽量避免使用整型数值。
  3. Enumerator range。 每个enumerator的value都存在一个range。如果对其初始化或赋值时超出了这个range,结果是undefined。因此从integer到enumeration之间必须是explicit conversion。为了保证可移植性,要避免赋integral value时超出range的情况发生。Range的确定方法见p.77.
  4. 一个hidden global name可以通过::来引用,一个hidden local name无法被引用。
  5. string literal 的类型是const char[n+1],n是字符个数。例如,"hello" 的类型是 const char[6]。在从前版本C/C++中,string literal的类型是char*,为了兼顾从前的大量程序,把一个string literal赋值给char*依然有效,但是修改const 字串的结果是undefined。例如,char* pstr="hello"; pstr[1]='t';
  6. 从一个函数中返回string literal是安全的,因为给string literal分配的空间不会因为函数的返回而被释放。例如,return "hello";
  7. 两个完全相同的string literal 是否只有一份copy,是implementation-defined. 例如,char* pstr1 = "hello"; char* pstr2="hello"; pstr1是否等于pstr2由具体实现决定的。
  8. 两个指针相减只有在他们指向同一个数组时,结果才被defined。指针相加没有意义、不被允许。
  9. "plain" T&类型的initializer必须是Lvalue,const T&类型的initializer不必是Lvalue甚至不必是类型T(存在implicit type conversion)。例如,int& i = 1;(error) const int& i = 1; (OK,产生的临时变量一直存在知道i的scope结束)。
  10. pointers to function,pointers to members不能赋值给void*
  11. structure对象的大小未必是其member大小之和。
(To be updated)

Sorted Duplicate Records

| No Comments | No TrackBacks
Berkeley DB engine supports duplicate records that share a key. The duplicate records can be sorted or unsorted, which depends on the flag is DB_DUP or DB_DUPSORT. If DB_DUPSORT value is selected, we should provide a sorting function. The engine does not accept two duplicate records is judged to be equal to each other by the sorting function. In other words, if two duplicate records are equal to each other, only one copy will be stored. However, this can't be accepted in some scenarios.
One solution for this problem is to adjust the return value of sorting function. For example,

int CompareRecord(Db *dbp, const Dbt *a, const Dbt *b)
{
    int ai, bi
    int ret = 0;
    memcpy(&ai, a->get_data(), sizeof(int));
    memcpy(&bi, b->get_data(), sizeof(int));
    ret = ai - bi;
    if(ret == 0)
        ret = -1;
    return ret;
}
In the codes above, if ai is equal to bi, the sorting function will return -1 instead of 0. Therefore, both ai and bi can be stored in the database.

Recent Comments

  • 北极冰仔: 上次你冬天我是夏天,现在你夏天,我冬天了 read more
  • Shawn: College 版 DMOZ 么?囧 read more
  • Jiang: 已经不分四季了:) read more
  • 老所: 同志,国内立冬了。 read more
  • 老所: 很幸福啊,:) read more