- 浏览: 251471 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
wilsonchen:
...
pdf.js html5展现pdf -
angole:
不错,又学习到了。
mybatis打印sql -
hft24dq:
rxcss66 写道 个人没有搞明白什么原理,不过跟一楼说的一 ...
mybatis打印sql -
fireinjava:
fireinjava 写道org.apache.ibatis. ...
mybatis打印sql -
fireinjava:
org.apache.ibatis.logging.LogFa ...
mybatis打印sql
原文地址:http://bloodredsun.com/?p=320
另一篇文章:What Every Computer Scientist Should Know About Floating-Point Arithmetichttp://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
My current client is in the middle of hiring some Java developers and as I
mentioned earlier in March (Interviewing – the importance of PASSION!) I’ve been doing some of the interviewing. One of the things we’ve done is to create a technical task to see how the candidates actually code. It’s a simple exercise that requires them to think through some of the basics of financial operations and one thing that has surprised me has been the common use of doubles to represent financial values. It’s been highlighted for some time that this is not a great thing to do but someone actually challenged me to show that it wasn’t.
So here we go…
view plaincopy to clipboardprint?
package com.bloodredsun;
public class DoubleOperation {
public static void main(String[] args) {
double t1 = 10.266d;
double t2 = 10.0d;
//Outputs 0.266
System.out.println(t1-t2);
double h1 = 100.266d;
double h2 = 100.0d;
//Outputs 0.26600000000000534
System.out.println(h1-h2);
}
}
Ouch! That is not what we want but it is the classic behaviour of doubles. The inability to represent some decimals in the IEEE-754 format (as binary fractions) causes this. If we want correct precision the answer is to use BigDecimals but we have to remember to use Strings in the constructors or you end up with the same issues that you were trying to avoid.
view plaincopy to clipboardprint?
package com.bloodredsun;
import java.math.BigDecimal;
public class BigDecimalOperation {
public static void main(String[] args) {
BigDecimal t1 = new BigDecimal("10.266");
BigDecimal t2 = new BigDecimal("10.0");
//Outputs 0.266
System.out.println(t1.subtract(t2));
BigDecimal h1 = new BigDecimal("100.266");
BigDecimal h2 = new BigDecimal("100.0");
//Outputs 0.266
System.out.println(h1.subtract(h2));
}
}
That’s great but wouldn’t it be nice to use the normal operators rather than the overly-verbose method calls for the mathematical operations.
Now there is no way that we can do this in Java but if we let ourselves use another language on the JVM…
view plaincopy to clipboardprint?
package com.bloodredsun
object ScalaBigDecimalOperation {
def main (args: Array[String]) {
var t1 = BigDecimal("10.266")
var t2 = BigDecimal("10.0")
//Outputs 0.266
println(t1 - t2)
var h1 = BigDecimal("100.266")
var h2 = BigDecimal("100.0")
//Outputs 0.266
println(h1 - h2)
}
}
Scala FTW!
PS if you want to know more about floating point operations have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic
另一篇文章:What Every Computer Scientist Should Know About Floating-Point Arithmetichttp://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
My current client is in the middle of hiring some Java developers and as I
mentioned earlier in March (Interviewing – the importance of PASSION!) I’ve been doing some of the interviewing. One of the things we’ve done is to create a technical task to see how the candidates actually code. It’s a simple exercise that requires them to think through some of the basics of financial operations and one thing that has surprised me has been the common use of doubles to represent financial values. It’s been highlighted for some time that this is not a great thing to do but someone actually challenged me to show that it wasn’t.
So here we go…
view plaincopy to clipboardprint?
package com.bloodredsun;
public class DoubleOperation {
public static void main(String[] args) {
double t1 = 10.266d;
double t2 = 10.0d;
//Outputs 0.266
System.out.println(t1-t2);
double h1 = 100.266d;
double h2 = 100.0d;
//Outputs 0.26600000000000534
System.out.println(h1-h2);
}
}
Ouch! That is not what we want but it is the classic behaviour of doubles. The inability to represent some decimals in the IEEE-754 format (as binary fractions) causes this. If we want correct precision the answer is to use BigDecimals but we have to remember to use Strings in the constructors or you end up with the same issues that you were trying to avoid.
view plaincopy to clipboardprint?
package com.bloodredsun;
import java.math.BigDecimal;
public class BigDecimalOperation {
public static void main(String[] args) {
BigDecimal t1 = new BigDecimal("10.266");
BigDecimal t2 = new BigDecimal("10.0");
//Outputs 0.266
System.out.println(t1.subtract(t2));
BigDecimal h1 = new BigDecimal("100.266");
BigDecimal h2 = new BigDecimal("100.0");
//Outputs 0.266
System.out.println(h1.subtract(h2));
}
}
That’s great but wouldn’t it be nice to use the normal operators rather than the overly-verbose method calls for the mathematical operations.
Now there is no way that we can do this in Java but if we let ourselves use another language on the JVM…
view plaincopy to clipboardprint?
package com.bloodredsun
object ScalaBigDecimalOperation {
def main (args: Array[String]) {
var t1 = BigDecimal("10.266")
var t2 = BigDecimal("10.0")
//Outputs 0.266
println(t1 - t2)
var h1 = BigDecimal("100.266")
var h2 = BigDecimal("100.0")
//Outputs 0.266
println(h1 - h2)
}
}
Scala FTW!
PS if you want to know more about floating point operations have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic
发表评论
-
spring send gmail
2012-04-24 11:06 1147只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1090原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1208原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5783情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 931http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1106几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1605原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1575原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3156总结:HPack 优于 CJSON json1 json2 ... -
http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
2011-06-27 10:48 1213原文地址:http://jbaruch.wordpress.c ... -
Sturts2 全局异常日志
2011-06-25 10:50 1811原文地址:http://www.brucephillips.n ... -
eclipse的build与clean
2011-06-22 09:01 1580现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 837图标各部分名字 -
jfreechart 自定义饼图颜色
2011-06-09 18:52 4901关键代码: private static class Pi ... -
jfreechart demo 代码
2011-06-07 18:33 2954jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 946基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 1020collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1080最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1589修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 20013mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
We are going to use Dagger2 to replace production code with Test Doubles. We are going to practice pair programming. Getting started This repository contains an Android application to show Super ...
测试双打演示 测试替身的演示文稿和代码 开始使用代码 克隆回购 运行bundle install 运行rspec 演示文稿入门 克隆回购 运行./server ... 按 P 开始演示 ...麻省理工学院许可证 (MIT) 版权所有 (c) [2015] [弗兰克·...
You’ll learn how to implement the dependency injection design pattern for writing test doubles, use concurrency in web applications, and create and consume JSON and XML in web services. Along the ...
Also, HDFS 3.0 announced GA for erasure coding, which doubles the storage efficiency of data and thus reduces the cost of storage for enterprise use cases. HDFS added support for multiple standby ...
You’ll learn how to implement the dependency injection design pattern for writing test doubles, use concurrency in web applications, and create and consume JSON and XML in web services. Along the ...
在.NET框架中,`ObservableCollection<T>`是一种常用的集合类,尤其在WPF和MVVM模式中,它允许我们在数据绑定时实时更新视图。当数据发生变化时,它会触发通知事件,使得UI能够自动更新。然而,对于包含可比较元素...
Please see the actual settings later in the document for more details as to why ; we recommend these changes in PHP's behavior. ; display_errors ; Default Value: On ; Development Value: On ; ...
You’ll learn to implement the dependency injection design sample for writing check doubles, use concurrency in net functions, and create and eat JSON and XML in net providers. Alongside...
We trust the material we have included will serve as a starting point for the many professionals who are beginning their journey into this exciting discipline. We begin our journey into the realm of ...
leading .NET architect Bradley Irby introduces proven best practices for revitalizing older .NET code and integrating new architectural and development advances into business-critical systems that can...
* [MNG-4941] - PluginDescriptorBuilder doesn't populate expression/default-value fields for mojo parameters * [MNG-4952] - [regression] RELEASE field of repository metadata is not updated upon ...
You'll learn how to implement the dependency injection design pattern for writing test doubles, use concurrency in web applications, and create and consume JSON and XML in web services. Along the way...
For the purpose of this document, we will focus on technical questions. #### 5. Please tell me about OOPs. **Answer**: OOPs stands for Object-Oriented Programming System. It is a programming ...
w [0]返回w的第一个字母一旦找到双精度数,就无需继续for循环 概念 单元1,单元2,用于循环,列出。 如何完成这项作业 为了完成此分配,您将需要在main.py文件中编写代码。 完成后,该作业将自动进行测试并评分。 ...