惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
月光博客
月光博客
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
B
Blog
C
Check Point Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
量子位
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - Jonny Yu

[GCC学习]get the optimized function call graph 界面开发中的那些疯狂的小事 Re:架构设计之性能设计经验 技术资产管理 .Net下进程外COM服务器的实现 控件的鼠标拖动和改变大小实现的思考 Learn from mistake, i.e. 和 e.g. 是不同的 How can I hide a user from the Welcome Screen in Windows XP? About HDC window class, OO about wParam and lParam [GDI+]DrawRectangle和FillRectangle,细节决定成败 Disable the CrossThreadChecking in .Net Framework v2.0 several useful Store Procedures in MSSQL http://cnblogs.com/rickie/archive/2005/07/02/184927.aspx VS2005使用体验 Inventor 的一些快捷键 First day in Hanna. XPath crash course note
就差一点点-微妙的强制类型转换
Jonny Yu · 2005-07-20 · via 博客园 - Jonny Yu

就差一点点-微妙的强制类型转换

今天在写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  阅读(1174)  评论(3)    收藏  举报