FastLight 2019-04-15 16:52 采纳率: 100%
浏览 864
已采纳

sqlite 中使用无符号整数存储的数据,再读取的时候,“溢出”的数据全变成浮点数了,怎么解决?

图片说明

谁能用php帮我解决掉这个问题,加分。

  • 写回答

5条回答 默认 最新

  • threenewbee 2019-04-15 17:06
    关注

    看你希望怎么解决,因为我们不知道你的约束条件在那里。但是可以做很多办法来补偿。

    比如说在数据库层面,用两个整数字段,或者字符串字段来存储单个的整数。
    比如说,数据库不能修改,但是可以通过编程语言做修正。

    需要你先给出你的场景,还有代码,然后才好找到进一步的方法

    C/C++直接用指针暴力转换就可以了。

    不同的编译器对64bit的整数的支持不一样,以下代码在gcc编译
    在线验证网址:https://c.runoob.com/compile/12

    #include <stdio.h>
    
    int main()
    {
        double d = -6.29187711124658e-311;
        __int64_t *l = (__int64_t *)&d;
        printf("%llu", *l);
        return 0;
    }
    

    9223384771755824298

    关键是Java,它不支持指针,所以没办法简单暴力,需要用BigInteger
    代码如下:

    import java.math.BigInteger;
    
    public class HelloWorld {
        public static void main(String []args) {
            double d = -6.29187711124658e-311;
            long value = Double.doubleToRawLongBits(d);
            BigInteger bi = new BigInteger("0");
            BigInteger base = new BigInteger("1");
            Boolean sign = (value < 0);
            if (value < 0)
            {
                value += 9223372036854775807L;
                value += 1L;
            }
            while (value > 0)
            {
                bi = bi.add(new BigInteger(String.valueOf(value % 2)).multiply(base));
                base = base.multiply(new BigInteger("2"));
                value /= 2;
            }
            if (sign) bi = bi.add(new BigInteger("9223372036854775808"));
            System.out.println(bi);
        }
    }
    

    9223384771755824298

    如果一种语言不支持 Double.doubleToRawLongBits 这样的方法
    那么你可以根据IEEE标准的浮点数的格式自己去转换。

    如果你还需要别的语言的例子,可以继续追问。
    如果问题解决,麻烦采纳下,谢谢

    下面是SQLite数据类型参考手册上关于这个问题的解释:

    datatype reference in SQLite
    INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
    That is, you can only store values from -2**63 to (2**63-1). What does SQLite do for a value outside of this range? As we saw earlier, it switches over into floating point. Again, quoting from the SQLite reference:
    REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
    Many programmers are familiar with this type under the name double.

    PHP版本

    <?php
    $binarydata = pack("d", -3.68861248304756e-311);
    $value = unpack("Q", $binarydata);
    echo("float format: ");
    echo($value[1] + pow(2, 64));
    echo("\ninteger format: ");
    $ofvalue = $value[1] + 9223372036854775808;
    echo (" 92233");
    echo ($ofvalue + 72036854775808);
    ?>
    

    float format: 9.2233795026896E+18
    integer format: 9223379502689557504

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!