弱数学技能:什么是 8 位分子和 8 位分母?

Weak Math Skillset: What is an 8-bit numerator and 8-bit denominator?

不好意思说我的数学能力很弱,但我是一名计算机科学专业的学生。我在 class 中,这让人不知所措。这是我的家庭作业的一部分,但是,在我理解这一部分之前我不能继续它。我 class 中的每个人都在努力编写某种方法来完成作业。我给我的教授发了邮件,要求说明该方法应该如何编写。

他说我应该存储一个 8 位分子和一个 8 位分母...但是...我 理解分子和分母是什么.有人可以给我解释一下吗?

这是我从教授那里得到的部分回复:

"[You're] supposed to encode the coefficients A, B, and C into the chromosome [chromosome is our interface, so chromosome chrome is our object]. A, B, and C are all numbers that can be fractional. You must store an 8-bit numerator and an 8-bit denominator. A, B, and C can also be positive or negative."

执行此操作后,我们应该将 A, B, and C 映射到二进制字符串以创建新的染色体对象。

我是如何翻译的:A, B, and C 将是双精度数(至少)保持一个十进制值。但在我知道 8 位分子和 8 位分母是什么之前,我无法实现它。请帮忙!

谢谢! <3 TG52

这是根据您的 post 得出的猜想,但我相信您的教授在这项作业中要求的是用 8 位有符号整数表示分数的分子和分母。

在这种情况下使用 byte 数据类型就足够了:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

所以你可以有一个 class 叫做 Fraction 像:

class Fraction{
    private byte numerator;
    private byte denominator;

    public Fraction(byte numerator, byte denominator){
        this.numerator = numerator;
        this.denominator = denominator;
    }
}