MindScape

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

今天在写UnitTest时发现有个测试死活通不过。
其中语句的关键代码是这样的:
float scale = 0.24f;
int actualWidth = 800;
int width = (int)(actualWidth * scale);
AssertEquals(192, width);

dotUnit报错, expected value 192, 191 was got.
但是如果这样写就对了。
float scale = 0.24f;
int actualWidth =  800;
float widthf = actualWidth * scale; // 这里是192.0
int width = (int)widthf; //这样就是192了。

最后改成了
int width = (int)(float)(actualWidth * scale);
搞定。

但是问题仍然存在,C#编译器(.Net runtime)对于这两种强制类型转换内部处理有何差异呢?
第一种强制类型转换的结果为什么不对?我还没有很明确的答案,有谁知道?



posted on 2005-07-20 16:21  Jonny Yu  阅读(1150)  评论(3编辑  收藏  举报