小编sto*_*ito的帖子

如何判断数字是否可以精确地用浮点数表示?

由于浮点数是基于 2 的数字系统,因此不可能0.24F直接表示为相同的数字,因此不可能在没有重复小数点的1/3十进制系统中表示,即或。1/3=0.3333...0.(3)

0.24F因此,当打印回十进制表示形式时,浮点数会0.23因四舍五入而发生变化:

println(0.24F) => 0.23999999463558197021484375
Run Code Online (Sandbox Code Playgroud)

while0.25F可以直接显示:

println(0.25F) => 0.25
Run Code Online (Sandbox Code Playgroud)

但是我如何确定一个数字可以完全表示呢?

isExactFloat(0.25F) ==> true
isExactFloat(0.24F) ==> false
Run Code Online (Sandbox Code Playgroud)

也许 Java API 已经有一些函数可以做到这一点?

UPD 这是一个代码,显示 [-4, 4] 范围内的浮点数及其内部表示:

public class FloatDestructure {
    public static void main(String[] args) {
        BigDecimal dec = BigDecimal.valueOf(-4000L, 3);
        BigDecimal incr = BigDecimal.valueOf(1L, 3);
        for (int i = 0; i <= 8000; i++) {
            double dbl = dec.doubleValue();
            floatDestuct(dbl, dec);
            dec = dec.add(incr);
        }

    } …
Run Code Online (Sandbox Code Playgroud)

java floating-point ieee-754

6
推荐指数
1
解决办法
2086
查看次数

minio etag是如何生成的

有谁知道当你PUT一个对象时minio etag是如何生成的?它是文件的哈希值吗?我们可以用它来防止同一文件上传两次吗?

非常感谢!

etag minio

6
推荐指数
1
解决办法
6927
查看次数

java.lang.Integer对象布局和它的开销

我使用了一个名为JOL(Java Object Layout)的工具,它试图分析对象布局.它带有一个cli,我用它来分析java.lang.Integer.我看到Integer对象占用了12个额外的字节用于开销.该开销可以是4个字节用于该对象所属的类的地址,另外4个用于垃圾收集,但剩余的4个字节呢?我知道对象有一个整数hashCode值,但我不认为它是唯一的(即它不使用内存的位置,它使用原始值代替)因为:

Integer a = new Integer(12);
Integer b = new Integer(12);

System.out.println(a.hashCode() == 12 && b.hashCode() == 12);
// prints: true
Run Code Online (Sandbox Code Playgroud)

日志:

$ java -jar jol-cli/target/jol-cli.jar internals java.lang.Integer
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# Running 64-bit HotSpot VM.
# Using compressed oop with 0-bit shift.
# Using compressed klass with 0-bit shift.
# Objects are 8 bytes …
Run Code Online (Sandbox Code Playgroud)

java integer jvm object-layout jol

4
推荐指数
1
解决办法
161
查看次数

如何将Joda-Time的DateTimeFormat.forStyle()转换为JSR 310 JavaTime?

我正在使用转换Grails Joda-Time插件到JavaTime.

我有这样的旧Joda时间码:

    def style
    switch (type) {
        case LocalTime:
            style = '-S'
            break
        case LocalDate:
            style = 'S-'
            break
        default:
            style = 'SS'
    }
    Locale locale = LocaleContextHolder.locale
    return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT)
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它转换为JSR 310?我找不到任何类似于接受样式的方法forStyle(String style).

UPD 我找到了解决方法:

        Locale locale = LocaleContextHolder.locale
        DateTimeFormatter formatter
        switch (type) {
            case LocalTime:
                formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
                break
            case LocalDate:
                formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale)
                break
            default:
                formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale)
        }
        return formatter
Run Code Online (Sandbox Code Playgroud)

但它失败的Instant类型.Spock规范重现:

def 'Instant locale formatting'() {
    given:
    Instant inst …
Run Code Online (Sandbox Code Playgroud)

migration jodatime java-8 java-time

1
推荐指数
1
解决办法
1817
查看次数

为什么javax.money.CurrencyUnit不能序列化?

为什么不javax.money.CurrencyUnit延伸java.io.Serializable?它的所有子类实现java.io.Serializable,并且最重要的是,如果你想在你的hibernate映射中使用它,那么findbugs会阻止你(非常正确)因为:

[INFO] Class com.mycompany.SiteEntity defines non-transient non-serializable instance field defaultCurrency [com.mycompany.SiteEntity] In SiteEntity.java
[INFO] Class com.mycompany.SiteEntity defines non-transient non-serializable instance field supportedCurrencies [com.mycompany.SiteEntity] In SiteEntity.java
[INFO] Class com.mycompany.UserEntity defines non-transient non-serializable instance field sessionCurrency [com.mycompany.UserEntity] In UserEntity.java
Run Code Online (Sandbox Code Playgroud)

这是否意味着必须JDKCurrencyAdapter在一个人的hibernate映射中使用该类?我更喜欢使用界面,但如果不可能,那么我将使用该类.

java hibernate jsr354 java-money

1
推荐指数
1
解决办法
376
查看次数