10年 Java程序员,硬核人生!勇往直前,永不退缩!

欢迎围观我的git:https://github.com/R1310328554/spring_security_learn 寻找志同道合的有志于研究技术的朋友,关注本人微信公众号: 觉醒的码农,或Q群 165874185

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

为什么无法 精准转换呢

        double doubleMaxValue = Double.MAX_VALUE;
        System.out.println("doubleMaxValue = " + doubleMaxValue);
        System.out.println("doubleMaxValue -1 = " + (doubleMaxValue - 1)); // 打印的结果和上面的完全一样
        System.out.println("doubleMaxValue equals = " + (doubleMaxValue - 1 == doubleMaxValue));// 竟然是 true

        double doubleMaxValue2 = (Long.MAX_VALUE / 2L) ;
        System.out.println("doubleMaxValue = " + doubleMaxValue2);
        System.out.println("doubleMaxValue -1 = " + (doubleMaxValue2 - 1)); // 打印的结果和上面的完全一样
        System.out.println("doubleMaxValue equals = " + (doubleMaxValue2 - 1 == doubleMaxValue2));// 竟然是 true

        Long longMaxValue = Long.MAX_VALUE;
        System.out.println("longMaxValue = " + longMaxValue);// 9223372036854775807
        System.out.println("doubleMaxValue -doubleMaxValue  = " + ( doubleMaxValue - longMaxValue));// 打印的结果和 Double.MAX_VALUE的完全一样, 减去相当于没有减..
        
        // 0x7fffffffffffffffL
        // 0x7fefffffffffffffL
        BigDecimal longBigDecimal = new BigDecimal(longMaxValue);// 和直接打印是一样的
        System.out.println("longBigDecimal = " + longBigDecimal);
        System.out.println("Double.longBitsToDouble(0x7fefffffffffffffL) = " + Double.longBitsToDouble(0x7fefffffffffffffL));
        BigDecimal doubleMinValue = new BigDecimal(Double.MIN_VALUE);
        System.out.println("doubleMinValue = " + doubleMinValue);// 4.9e-324: 4.940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625E-324
        BigDecimal doubleBigDecimal = new BigDecimal(doubleMaxValue);
        System.out.println("doubleBigDecimal = " + doubleBigDecimal);// 308位数字,10的308次方: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

        double doubleMaxValue2a = doubleMaxValue - 1;
        BigDecimal bigDecimal2 = new BigDecimal(doubleMaxValue2a);
        System.out.println("bigDecimal2 = " + bigDecimal2);// 和doubleBigDecimal 结果 是一样的!

        if (doubleMaxValue == doubleMaxValue2a) {
            System.out.println("doubleMaxValue2a ==== " + doubleMaxValue2a); // doubleMaxValue - 1 和 doubleMaxValue 完全一样..
        }

        BigDecimal bigDecimal3 = new BigDecimal(Float.MAX_VALUE);
        System.out.println("bigDecimal3 = " + bigDecimal3);//  3.4028235e+38f:  340282346638528859811704183484516925440

        BigDecimal bigDecimal3a = new BigDecimal(Float.MIN_VALUE);
        System.out.println("bigDecimalMin = " + bigDecimal3a);// 1.4e-45f: 1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125E-45

        double nan = Double.longBitsToDouble(0xfff0000000000001L); // longBitsToDouble(0x7fefffffffffffffL).
        System.out.println("longBitsToDouble = " + nan); // 竟然是 NaN , why ? 超过了最大值?
        double positiveInfinity = Double.longBitsToDouble(0x7ff0000000000000L);
        System.out.println("positiveInfinity = " + positiveInfinity);
        double positiveInfinity2 = Double.longBitsToDouble(0xfff0000000000000L);
        System.out.println("positiveInfinity2 = " + positiveInfinity2);

        long number = 499999999000000001L;
        double converted = (double) number;
        System.out.println( "long - (double) converted " + ( number - (long) converted )); // 精度丢失1
        System.out.println( "long -   converted " + ( number -  converted ));
        Long i = 1000000123456789012L;
        System.out.println("Long = " + i); // Long 永远不会丢失精度!
        String s = i + "";
        /*
            float的精度是6位有效数字,取值范围是10的-38次方到10的38次方,float占用4字节空间
            double的精度是15位有效数字,取值范围是10的-308次方到10的308次方,double占用8字节空间。
         */
        Double d = Double.parseDouble(s);
        System.out.println("Double = " + d);// 100000012345678 都是准确的, 901 后面的精度全部丢失!!   Double 的精度是;
        // 1.00000012345678896E17 的后面的 896是怎么来的?

        Float f = Float.parseFloat(s);
        System.out.println("Float = " + f);


        double v = Double.longBitsToDouble(i);
        System.out.println("Double longBitsToDouble = " + v);


        Double dd = 10000001234567890123D;
        System.out.println("Double " + dd);// 1.000000123456789E19 和字面值不一样!
        double v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);// 1.000000123456789E19

        dd = 123456789876543219123456789D;
        System.out.println("Double " + dd);//dd = 1.2345678987654321E26, 小数位16位,而 精度最多是15位; 而且貌似也并不会 四舍五入!
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);

        dd = 123456789876543D;
        System.out.println("Double " + dd);//dd = 1.
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);


        dd = 12345678.9876543210D;
        System.out.println("Double " + dd);//dd = 1.
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);

        dd = 12345678.9876543280D;
        System.out.println("Double " + dd);//dd = 1.
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);

        dd = 12345678.9876543289D;
        System.out.println("Double " + dd);//dd = 1.
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);


        dd = 12345678.9876543219876543D;
        System.out.println("Double " + dd);//dd = 1.2345678987654323E7
        v1 = BigDecimal.valueOf(dd).doubleValue();
        System.out.println("BigDecimal doubleValue " + v1);

        float f1 = 98765432123456789f;
        System.out.println("float = " + f1); // 9.8765436E16 , 在5432 的位置 失去精度

        f1 = 98765456789f;
        System.out.println("float = " + f1);// 9.8765455E10 小数位7位,而 精度最多是6位;

 

打印

D:\devTool\jdk1.8.0_161\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:64828,suspend=y,server=n -Xms1048m -Xmx1048m -javaagent:C:\Users\lk\AppData\Local\JetBrains\IntelliJIdea2021.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\devTool\jdk1.8.0_161\jre\lib\charsets.jar;D:\devTool\jdk1.8.0_161\jre\lib\deploy.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\dnsns.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\jaccess.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\localedata.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\nashorn.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\sunec.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;D:\devTool\jdk1.8.0_161\jre\lib\ext\zipfs.jar;D:\devTool\jdk1.8.0_161\jre\lib\javaws.jar;D:\devTool\jdk1.8.0_161\jre\lib\jce.jar;D:\devTool\jdk1.8.0_161\jre\lib\jfr.jar;D:\devTool\jdk1.8.0_161\jre\lib\jfxswt.jar;D:\devTool\jdk1.8.0_161\jre\lib\jsse.jar;D:\devTool\jdk1.8.0_161\jre\lib\management-agent.jar;D:\devTool\jdk1.8.0_161\jre\lib\plugin.jar;D:\devTool\jdk1.8.0_161\jre\lib\resources.jar;D:\devTool\jdk1.8.0_161\jre\lib\rt.jar;D:\code\git\mw\umc-spgate-sms\target\test-classes;D:\code\git\mw\umc-spgate-sms\target\classes;C:\Users\lk\.m2\repository\com\montnets\umc\spgate-std\1.0.0.1\spgate-std-1.0.0.1.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter\2.4.2\spring-boot-starter-2.4.2.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.2\spring-boot-autoconfigure-2.4.2.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.4.2\spring-boot-starter-logging-2.4.2.jar;C:\Users\lk\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\lk\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\lk\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\lk\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;C:\Users\lk\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\lk\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\lk\.m2\repository\org\springframework\spring-core\5.3.3\spring-core-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-jcl\5.3.3\spring-jcl-5.3.3.jar;C:\Users\lk\.m2\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;C:\Users\lk\.m2\repository\com\montnets\umc\common\1.0.0.1\common-1.0.0.1.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.4.2\spring-boot-starter-web-2.4.2.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.4.2\spring-boot-starter-json-2.4.2.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.2\spring-boot-starter-tomcat-2.4.2.jar;C:\Users\lk\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;C:\Users\lk\.m2\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\Users\lk\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;C:\Users\lk\.m2\repository\org\springframework\spring-webmvc\5.3.3\spring-webmvc-5.3.3.jar;C:\Users\lk\.m2\repository\org\apache\commons\commons-lang3\3.11\commons-lang3-3.11.jar;C:\Users\lk\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Users\lk\.m2\repository\com\belerweb\pinyin4j\2.5.1\pinyin4j-2.5.1.jar;C:\Users\lk\.m2\repository\org\quartz-scheduler\quartz\2.3.2\quartz-2.3.2.jar;C:\Users\lk\.m2\repository\com\mchange\mchange-commons-java\0.2.15\mchange-commons-java-0.2.15.jar;C:\Users\lk\.m2\repository\org\apache\httpcomponents\httpclient\4.5.13\httpclient-4.5.13.jar;C:\Users\lk\.m2\repository\org\apache\rocketmq\rocketmq-client\4.7.0\rocketmq-client-4.7.0.jar;C:\Users\lk\.m2\repository\org\apache\rocketmq\rocketmq-common\4.7.0\rocketmq-common-4.7.0.jar;C:\Users\lk\.m2\repository\org\apache\rocketmq\rocketmq-remoting\4.7.0\rocketmq-remoting-4.7.0.jar;C:\Users\lk\.m2\repository\org\apache\rocketmq\rocketmq-logging\4.7.0\rocketmq-logging-4.7.0.jar;C:\Users\lk\.m2\repository\io\netty\netty-tcnative-boringssl-static\2.0.36.Final\netty-tcnative-boringssl-static-2.0.36.Final.jar;C:\Users\lk\.m2\repository\commons-validator\commons-validator\1.6\commons-validator-1.6.jar;C:\Users\lk\.m2\repository\commons-beanutils\commons-beanutils\1.9.2\commons-beanutils-1.9.2.jar;C:\Users\lk\.m2\repository\commons-digester\commons-digester\1.8.1\commons-digester-1.8.1.jar;C:\Users\lk\.m2\repository\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;C:\Users\lk\.m2\repository\org\redisson\redisson\3.12.0\redisson-3.12.0.jar;C:\Users\lk\.m2\repository\io\netty\netty-common\4.1.58.Final\netty-common-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-buffer\4.1.58.Final\netty-buffer-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-resolver-dns\4.1.58.Final\netty-resolver-dns-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-codec-dns\4.1.58.Final\netty-codec-dns-4.1.58.Final.jar;C:\Users\lk\.m2\repository\javax\cache\cache-api\1.1.1\cache-api-1.1.1.jar;C:\Users\lk\.m2\repository\io\projectreactor\reactor-core\3.4.2\reactor-core-3.4.2.jar;C:\Users\lk\.m2\repository\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;C:\Users\lk\.m2\repository\io\reactivex\rxjava2\rxjava\2.2.20\rxjava-2.2.20.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.11.4\jackson-dataformat-yaml-2.11.4.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar;C:\Users\lk\.m2\repository\net\bytebuddy\byte-buddy\1.10.19\byte-buddy-1.10.19.jar;C:\Users\lk\.m2\repository\org\jodd\jodd-bean\5.0.13\jodd-bean-5.0.13.jar;C:\Users\lk\.m2\repository\org\jodd\jodd-core\5.0.13\jodd-core-5.0.13.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-data-redis\2.4.2\spring-boot-starter-data-redis-2.4.2.jar;C:\Users\lk\.m2\repository\org\springframework\data\spring-data-redis\2.4.3\spring-data-redis-2.4.3.jar;C:\Users\lk\.m2\repository\org\springframework\data\spring-data-keyvalue\2.4.3\spring-data-keyvalue-2.4.3.jar;C:\Users\lk\.m2\repository\org\springframework\data\spring-data-commons\2.4.3\spring-data-commons-2.4.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-tx\5.3.3\spring-tx-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-oxm\5.3.3\spring-oxm-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-context-support\5.3.3\spring-context-support-5.3.3.jar;C:\Users\lk\.m2\repository\redis\clients\jedis\3.3.0\jedis-3.3.0.jar;C:\Users\lk\.m2\repository\org\apache\commons\commons-pool2\2.9.0\commons-pool2-2.9.0.jar;C:\Users\lk\.m2\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus-extension\3.4.3\mybatis-plus-extension-3.4.3.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus-core\3.4.3\mybatis-plus-core-3.4.3.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus-annotation\3.4.3\mybatis-plus-annotation-3.4.3.jar;C:\Users\lk\.m2\repository\com\github\jsqlparser\jsqlparser\4.0\jsqlparser-4.0.jar;C:\Users\lk\.m2\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;C:\Users\lk\.m2\repository\org\roaringbitmap\RoaringBitmap\0.9.15\RoaringBitmap-0.9.15.jar;C:\Users\lk\.m2\repository\org\roaringbitmap\shims\0.9.15\shims-0.9.15.jar;C:\Users\lk\.m2\repository\com\carrotsearch\java-sizeof\0.0.5\java-sizeof-0.0.5.jar;C:\Users\lk\.m2\repository\com\montnets\umc\umc-bill\1.0.0.1\umc-bill-1.0.0.1.jar;C:\Users\lk\.m2\repository\com\montnets\umc\umc-cache\1.0.0.1\umc-cache-1.0.0.1.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus-boot-starter\3.4.3\mybatis-plus-boot-starter-3.4.3.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus\3.4.3\mybatis-plus-3.4.3.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.4.2\spring-boot-starter-jdbc-2.4.2.jar;C:\Users\lk\.m2\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\Users\lk\.m2\repository\org\springframework\spring-jdbc\5.3.3\spring-jdbc-5.3.3.jar;C:\Users\lk\.m2\repository\com\baomidou\mybatis-plus-generator\3.5.0\mybatis-plus-generator-3.5.0.jar;C:\Users\lk\.m2\repository\org\mybatis\mybatis-typehandlers-jsr310\1.0.2\mybatis-typehandlers-jsr310-1.0.2.jar;C:\Users\lk\.m2\repository\com\alibaba\druid-spring-boot-starter\1.1.10\druid-spring-boot-starter-1.1.10.jar;C:\Users\lk\.m2\repository\com\alibaba\druid\1.1.10\druid-1.1.10.jar;C:\Users\lk\.m2\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar;C:\Users\lk\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\Users\lk\.m2\repository\cn\hutool\hutool-all\5.5.4\hutool-all-5.5.4.jar;C:\Users\lk\.m2\repository\com\montnets\umc\protocol-unified\1.0.0.1\protocol-unified-1.0.0.1.jar;C:\Users\lk\.m2\repository\com\montnets\umc\http-net-std\1.0.0.1\http-net-std-1.0.0.1.jar;C:\Users\lk\.m2\repository\io\netty\netty-transport-native-epoll\4.1.58.Final\netty-transport-native-epoll-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-transport-native-unix-common\4.1.58.Final\netty-transport-native-unix-common-4.1.58.Final.jar;C:\Users\lk\.m2\repository\com\montnets\umc\protocol-sms-std-ex\1.0.0.1\protocol-sms-std-ex-1.0.0.1.jar;C:\Users\lk\.m2\repository\com\montnets\umc\protocol-sms-interface\1.0.0.1\protocol-sms-interface-1.0.0.1.jar;C:\Users\lk\.m2\repository\com\montnets\umc\protocol-sms-std\1.0.0.1\protocol-sms-std-1.0.0.1.jar;C:\Users\lk\.m2\repository\org\springframework\spring-aop\5.3.3\spring-aop-5.3.3.jar;C:\Users\lk\.m2\repository\de\ruedigermoeller\fst\2.48-jdk-6\fst-2.48-jdk-6.jar;C:\Users\lk\.m2\repository\org\javassist\javassist\3.19.0-GA\javassist-3.19.0-GA.jar;C:\Users\lk\.m2\repository\org\objenesis\objenesis\2.4\objenesis-2.4.jar;C:\Users\lk\.m2\repository\com\cedarsoftware\java-util\1.9.0\java-util-1.9.0.jar;C:\Users\lk\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\lk\.m2\repository\nekohtml\nekohtml\1.9.6.2\nekohtml-1.9.6.2.jar;C:\Users\lk\.m2\repository\xerces\xercesImpl\2.8.1\xercesImpl-2.8.1.jar;C:\Users\lk\.m2\repository\xml-apis\xml-apis\1.3.03\xml-apis-1.3.03.jar;C:\Users\lk\.m2\repository\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;C:\Users\lk\.m2\repository\org\freemarker\freemarker\2.3.30\freemarker-2.3.30.jar;C:\Users\lk\.m2\repository\org\springframework\spring-test\5.3.3\spring-test-5.3.3.jar;C:\Users\lk\.m2\repository\org\apache\httpcomponents\httpclient-cache\4.5.13\httpclient-cache-4.5.13.jar;C:\Users\lk\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lk\.m2\repository\org\apache\httpcomponents\httpmime\4.5.13\httpmime-4.5.13.jar;C:\Users\lk\.m2\repository\org\apache\httpcomponents\httpcore\4.4.14\httpcore-4.4.14.jar;C:\Users\lk\.m2\repository\io\netty\netty-transport\4.1.58.Final\netty-transport-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-resolver\4.1.58.Final\netty-resolver-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-codec\4.1.58.Final\netty-codec-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-handler-proxy\4.1.58.Final\netty-handler-proxy-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-codec-socks\4.1.58.Final\netty-codec-socks-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-codec-http\4.1.58.Final\netty-codec-http-4.1.58.Final.jar;C:\Users\lk\.m2\repository\io\netty\netty-handler\4.1.58.Final\netty-handler-4.1.58.Final.jar;C:\Users\lk\.m2\repository\com\sleepycat\je\5.0.73\je-5.0.73.jar;C:\Users\lk\.m2\repository\commons-io\commons-io\2.5\commons-io-2.5.jar;C:\Users\lk\.m2\repository\org\opensmpp\opensmpp-core\3.0.0\opensmpp-core-3.0.0.jar;C:\Users\lk\.m2\repository\org\opensmpp\opensmpp-charset\3.0.0\opensmpp-charset-3.0.0.jar;C:\Users\lk\.m2\repository\com\xuxueli\xxl-job-core\2.3.0\xxl-job-core-2.3.0.jar;C:\Users\lk\.m2\repository\io\netty\netty-all\4.1.58.Final\netty-all-4.1.58.Final.jar;C:\Users\lk\.m2\repository\org\codehaus\groovy\groovy\2.5.14\groovy-2.5.14.jar;C:\Users\lk\.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\lk\.m2\repository\org\mybatis\mybatis\3.5.7\mybatis-3.5.7.jar;C:\Users\lk\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot-starter\2.1.0\jasypt-spring-boot-starter-2.1.0.jar;C:\Users\lk\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot\2.1.0\jasypt-spring-boot-2.1.0.jar;C:\Users\lk\.m2\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-swagger2\2.8.0\springfox-swagger2-2.8.0.jar;C:\Users\lk\.m2\repository\io\swagger\swagger-annotations\1.5.14\swagger-annotations-1.5.14.jar;C:\Users\lk\.m2\repository\io\swagger\swagger-models\1.5.14\swagger-models-1.5.14.jar;C:\Users\lk\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-spi\2.8.0\springfox-spi-2.8.0.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-core\2.8.0\springfox-core-2.8.0.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-schema\2.8.0\springfox-schema-2.8.0.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-swagger-common\2.8.0\springfox-swagger-common-2.8.0.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-spring-web\2.8.0\springfox-spring-web-2.8.0.jar;C:\Users\lk\.m2\repository\org\reflections\reflections\0.9.11\reflections-0.9.11.jar;C:\Users\lk\.m2\repository\com\google\guava\guava\20.0\guava-20.0.jar;C:\Users\lk\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\lk\.m2\repository\org\springframework\plugin\spring-plugin-core\1.2.0.RELEASE\spring-plugin-core-1.2.0.RELEASE.jar;C:\Users\lk\.m2\repository\org\springframework\spring-beans\5.3.3\spring-beans-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-context\5.3.3\spring-context-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\spring-expression\5.3.3\spring-expression-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\plugin\spring-plugin-metadata\1.2.0.RELEASE\spring-plugin-metadata-1.2.0.RELEASE.jar;C:\Users\lk\.m2\repository\org\mapstruct\mapstruct\1.2.0.Final\mapstruct-1.2.0.Final.jar;C:\Users\lk\.m2\repository\io\springfox\springfox-swagger-ui\2.8.0\springfox-swagger-ui-2.8.0.jar;C:\Users\lk\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;C:\Users\lk\.m2\repository\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;C:\Users\lk\.m2\repository\org\springframework\cloud\spring-cloud-starter-openfeign\3.0.0\spring-cloud-starter-openfeign-3.0.0.jar;C:\Users\lk\.m2\repository\org\springframework\cloud\spring-cloud-starter\3.0.0\spring-cloud-starter-3.0.0.jar;C:\Users\lk\.m2\repository\org\springframework\cloud\spring-cloud-context\3.0.0\spring-cloud-context-3.0.0.jar;C:\Users\lk\.m2\repository\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;C:\Users\lk\.m2\repository\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;C:\Users\lk\.m2\repository\org\springframework\cloud\spring-cloud-openfeign-core\3.0.0\spring-cloud-openfeign-core-3.0.0.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-starter-aop\2.4.2\spring-boot-starter-aop-2.4.2.jar;C:\Users\lk\.m2\repository\org\aspectj\aspectjweaver\1.9.6\aspectjweaver-1.9.6.jar;C:\Users\lk\.m2\repository\io\github\openfeign\form\feign-form-spring\3.8.0\feign-form-spring-3.8.0.jar;C:\Users\lk\.m2\repository\io\github\openfeign\form\feign-form\3.8.0\feign-form-3.8.0.jar;C:\Users\lk\.m2\repository\commons-fileupload\commons-fileupload\1.4\commons-fileupload-1.4.jar;C:\Users\lk\.m2\repository\org\springframework\spring-web\5.3.3\spring-web-5.3.3.jar;C:\Users\lk\.m2\repository\org\springframework\cloud\spring-cloud-commons\3.0.0\spring-cloud-commons-3.0.0.jar;C:\Users\lk\.m2\repository\org\springframework\security\spring-security-crypto\5.4.2\spring-security-crypto-5.4.2.jar;C:\Users\lk\.m2\repository\io\github\openfeign\feign-core\10.10.1\feign-core-10.10.1.jar;C:\Users\lk\.m2\repository\io\github\openfeign\feign-slf4j\10.10.1\feign-slf4j-10.10.1.jar;C:\Users\lk\.m2\repository\org\projectlombok\lombok\1.18.14\lombok-1.18.14.jar;C:\Users\lk\.m2\repository\junit\junit\4.13.1\junit-4.13.1.jar;C:\Users\lk\.m2\repository\org\hamcrest\hamcrest-core\2.2\hamcrest-core-2.2.jar;C:\Users\lk\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot-test\2.4.2\spring-boot-test-2.4.2.jar;C:\Users\lk\.m2\repository\org\springframework\boot\spring-boot\2.4.2\spring-boot-2.4.2.jar;C:\Users\lk\.m2\repository\com\github\ben-manes\caffeine\caffeine\2.8.8\caffeine-2.8.8.jar;C:\Users\lk\.m2\repository\org\checkerframework\checker-qual\3.8.0\checker-qual-3.8.0.jar;C:\Users\lk\.m2\repository\com\google\errorprone\error_prone_annotations\2.4.0\error_prone_annotations-2.4.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2021.1\lib\idea_rt.jar" test.lk.TestColl
Connected to the target VM, address: '127.0.0.1:64828', transport: 'socket'
l2 = 1053294592
l1 = 1025821608
l = 1053294592
doubleMaxValue = 1.7976931348623157E308
doubleMaxValue -1 = 1.7976931348623157E308
doubleMaxValue equals = true
doubleMaxValue = 4.6116860184273879E18
doubleMaxValue -1 = 4.6116860184273879E18
doubleMaxValue equals = true
longMaxValue = 9223372036854775807
doubleMaxValue -doubleMaxValue  = 1.7976931348623157E308
longBigDecimal = 9223372036854775807
Double.longBitsToDouble(0x7fefffffffffffffL) = 1.7976931348623157E308
doubleMinValue = 4.940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625E-324
doubleBigDecimal = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
bigDecimal2 = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
doubleMaxValue2a ==== 1.7976931348623157E308
bigDecimal3 = 340282346638528859811704183484516925440
bigDecimalMin = 1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125E-45
longBitsToDouble = NaN
positiveInfinity = Infinity
positiveInfinity2 = -Infinity
long - (double) converted 1
long -   converted 0.0
Long = 1000000123456789012
Double = 1.00000012345678899E18
Float = 1.00000012E18
Double longBitsToDouble = 7.833158944544174E-242
Double 1.000000123456789E19
BigDecimal doubleValue 1.000000123456789E19
Double 1.2345678987654321E26
BigDecimal doubleValue 1.2345678987654321E26
Double 1.23456789876543E14
BigDecimal doubleValue 1.23456789876543E14
Double 1.234567898765432E7
BigDecimal doubleValue 1.234567898765432E7
Double 1.2345678987654328E7
BigDecimal doubleValue 1.2345678987654328E7
Double 1.2345678987654328E7
BigDecimal doubleValue 1.2345678987654328E7
Double 1.2345678987654323E7
BigDecimal doubleValue 1.2345678987654323E7
float = 9.8765436E16
float = 9.8765455E10
Disconnected from the target VM, address: '127.0.0.1:64828', transport: 'socket'

Process finished with exit code 0

 

 

 

Double.MAX_VALUE 是 308位数字,10的308次方: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

 

但是精度远不够308, 而是15

 

但是

double 无法精确表示long,虽然double 的最大值比long的最大值 大, 但是不行。

 

 

关于 ZSET  的问题

zset 的分数是double 类型的, 当然, 传一个long 值进去, 也不会报错,但是呢, 可能会出现精度丢失, 从而导致 很多 value 一个分数的问题!

zrangeByScoreWithScores 

比如这样的 score,就不行了: 7404750117679876609D

package test.lk;

import com.montnets.umc.common.constant.RedisTopicConstant;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Tuple;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
 
public class TestJedisCluster {
    public static void main(String[] args) throws IOException {
        
        // 创建集群的节点集合
        Set<HostAndPort> nodes = new HashSet<>();
        
        // 添加节点到集群中
        String host = "127.0.0.1";
        host = "192.169.6.157:7003,192.169.6.157:7004,192.169.6.157:7005";
        host = "192.169.6.157";
        nodes.add(new HostAndPort(host, 7003));
        nodes.add(new HostAndPort(host, 7004));
        nodes.add(new HostAndPort(host, 7005));
//        nodes.add(new HostAndPort(host, 6382));
//        nodes.add(new HostAndPort(host, 6383));
//        nodes.add(new HostAndPort(host, 6384));
//        nodes.add(new HostAndPort(host, 6385));
//        nodes.add(new HostAndPort(host, 6386));
        
        // 读取Redis Pool的配置文件(位于classpath目录下)
//        InputStream _is = TestJedisCluster.class.getClassLoader().getResourceAsStream("redis_pool.properties");
//        Properties props = new Properties();
//        props.load(_is);
        
        // 读取配置项
//        int maxTotal = Integer.parseInt(props.getProperty("redis_maxTotal"));
//        int maxIdle = Integer.parseInt(props.getProperty("redis_maxIdle"));
//        int MinIdle = Integer.parseInt(props.getProperty("redis_minIdle"));
        
        // 设置Redis Pool相关参数
        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        poolConfig.setMaxTotal(maxTotal);
//        poolConfig.setMaxIdle(maxIdle);
//        poolConfig.setMinIdle(MinIdle);
        
        // 利用上面的集群节点nodes和poolConfig,创建redis集群连接池,并获取一个redis连接
        JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);
        
        // 利用获取的jedisCluster可以进行jedis的所有操作
        System.out.println(jedisCluster.set("name", "beyond"));
        System.out.println(jedisCluster.get("age"));
        System.out.println(jedisCluster.get("name"));

        double spMsgIdDo = 7403613772412279385D;
        double spMsgIdDo2 = 7403613772412279385D;
        long zcount = jedisCluster.zcount(RedisTopicConstant.TOPIC_SMS_MT_AWAIT_RPT_ZSET + "1", spMsgIdDo, spMsgIdDo2 );
        System.out.println("zcount = " + zcount);// 竟然返回是 307, 为什么有这么多数据? 前面刚刚zadd 进去的, 而且分数竟然是 7.4036137724122798E18

        Set<Tuple> zrangeByScore = jedisCluster.zrangeByScoreWithScores(RedisTopicConstant.TOPIC_SMS_MT_AWAIT_RPT_ZSET + "1", spMsgIdDo, spMsgIdDo2 );
        System.out.println("zrangeByScore = " + zrangeByScore);

        Set<Tuple> tuples = jedisCluster.zrangeByScoreWithScores(RedisTopicConstant.TOPIC_SMS_MT_AWAIT_RPT_ZSET + "1", spMsgIdDo, spMsgIdDo2);
        System.out.println("tuples = " + tuples);

//        Set<String> keys = jedisCluster.keys("umc:sms:mt_await_rpt_zset1*"); // JedisCluster only supports KEYS commands with patterns containing hash-tags ( curly-brackets enclosed strings )
//        System.out.println("keys = " + keys);


        Long aa = jedisCluster.zadd("test.zset1", Integer.MAX_VALUE - 1, "MAX_VALUE -");

        Double sc1 = Integer.MAX_VALUE + 4D;
        System.out.println("sc1 = " + sc1);
        System.out.println("sc1 = " + (sc1 - Integer.MAX_VALUE));
        aa = jedisCluster.zadd("test.zset1", Integer.MAX_VALUE + 1, "MAX_VALUE +");
        aa = jedisCluster.zadd("test.zset1", Integer.MAX_VALUE , "MAX_VALUE");
        aa = jedisCluster.zadd("test.zset1", 7404750117679876609D, "bb");
        aa = jedisCluster.zadd("test.zset1", 7404750117679876601D, "cc");
        aa = jedisCluster.zadd("test.zset1", 7404750117679876690D, "dd");

        System.out.println("tuples = " + aa);


        Set<Tuple> strings = jedisCluster.zrangeByScoreWithScores("test.zset1", Integer.MAX_VALUE-1, Integer.MAX_VALUE);
        System.out.println("strings = " + strings);

        strings = jedisCluster.zrangeByScoreWithScores("test.zset1", Integer.MAX_VALUE - 1D, sc1 + 1);
        System.out.println("strings = " + strings);

        // 归还连接
        jedisCluster.close();

        long lo = Long.parseLong("7404750117679876690");
//        long lo = Long.parseLong("9223372036854775807");
        System.out.println(" --- " + lo);

        String s = "7403613772412279385";
        s = "74036137724122751";
        double v = Double.parseDouble(s);
        double v2 = Double.valueOf(s);

    }
}

 

郁闷

 

 

浮点数啊, 要小心!

 

 

 

...

 

posted on 2021-09-28 20:56  CanntBelieve  阅读(164)  评论(0编辑  收藏  举报