kaist-cp / cs220

268 stars 53 forks source link

[Assignment 9] Question about Big int #428

Open oRE-o opened 4 days ago

oRE-o commented 4 days ago

Related Issue

No response

Googling Result

nothing

ChatGPT Result

nothing

Your question here

Hello, I'm doing Assignment 9, BigInt.

I have some questions about this.

Q1. Why this can be happen?

/// and the vector `vec![u32::MAX - 5, u32::MAX - 7]` represents the integer
/// `- (5 * 2^32 + 8)`

-> i think it means we have to implement ("signed-int like" calculated value of each carrier element) 2^(32 x) and sum it then i think it has to be *(-6) 2^32 + (-8)**. does it right?


Q2. I cannot understand the test case.

assert_eq!(
            "0000000080000000",
            format!("{}", BigInt::new(i32::MAX as u32) + BigInt::new(1))
        );

Case 1: If my question Q1 is right, it does not make sense because It means true value of (i32::MAX + 1) == 0 * 2^32 - 2^31, which means -2^31.

it should be 0000000180000000 which means (1 * 2^32) - 2^31 == 2^31 == true value of (i32::MAX + 1)

Case 2: If the Q1 is not right () it make sense because It means i32::MAX + 1 == 0x0000000080000000 (the sign bit moved to the left)

but in this case, the explanation of the "bigint.rs" does not make sense because

/// and the vector `vec![u32::MAX - 5, u32::MAX - 7]` represents the integer
/// `- (5 * 2^32 + 8)`

it should be *(-6) 2^32 + (u32::MAX-7)*, neither (-6 2^32) + (-8) nor (-5) * 2^32 + 8

I spend lots of time and keep searched another Big Int implementations but I cannot understand :( I want to understand about assignment 9's big int implementation.

oRE-o commented 4 days ago

Plus, About the matmul test,

fn matmul_test() {
        let mat1 = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]];
        let mat2 = vec![
            vec![7.0, 8.0, 9.0],
            vec![10.0, 11.0, 12.0],
            vec![13.0, 14.0, 15.0],
            vec![16.0, 17.0, 18.0],
        ];
        let ans = vec![
            vec![50.0, 68.0, 86.0, 104.0],
            vec![122.0, 167.0, 212.0, 257.0],
        ];
        let res = matmul(&mat1, &mat2);
        assert_eq!(ans, res);
..

because of this test case, i cannot pass.. first one seems to 2x3 matrix, and next is 4x3 matrix, and those cant be multiplied normally how can i pass the test case? do i have to implement transpose?

aquacat1220 commented 4 days ago
/// Matrix multiplication
///
/// You don't know how to multiply matrix?
/// Quite simple! See <https://www.mathsisfun.com/algebra/matrix-multiplying.html>
///
/// Assume rhs is transposed
/// - lhs: (m, n)
/// - rhs: (p, n)
/// - output: (m, p)
/// ...

As you can see in the comments for matmul(), you can assume rhs to be transposed. You don't have to implement transpose!

aquacat1220 commented 4 days ago

Think of BigInt as a 32 * len(carrier)-bit signed integer.

So BigInt::new_large(vec![u32::MAX - 5, u32::MAX - 7]) should be 0xFFFFFFFA FFFFFFF8, which is - (5 * 2^32 + 8). You can think of it as - (5 * 2^32 + 7) - 1, if that fits you better.

Sorry for the poor explanation.. I wish this comment cleared up (at least) some ambiguity!

oRE-o commented 4 days ago

OK! Thank you for your support, and I'll find my way to solve the problems. thanks again