2.67-给你一个任务,编写一个过程int_ size_ is_ 32(), 当在一个int是32位的机器上运行时,该程序产生1,而其他情况则产生0。不允许使用sizeof运算符。下面是开始时的尝试:

接题目:

/* The following code does not run properly on some machines */
int bad_int_size_is_32() {
	/* Set most significant bit (msb) of 32-bit machine */
	int set_msb = 1 << 31;
	/* Shift past msb of 32-bit word */
	int beyond_msb = 1 << 32;
	/* set_ msb is nonzero when word size >= 32
	beyond_msb is zero when word size <= 32 */
	return set_msb && !beyond_msb;
}

当在SUN SPARC这样的32位机器上编译并运行时,这个过程返回的却是0。下面的编译器
信息给了我们一个问题的指示:
warning: left shift count >= width of type
A.我们的代码在哪个方面没有遵守C语言标准?
B.修改代码,使得它在int至少为32位的任何机器上都能正确地运行。
C.修改代码,使得它在int至少为16 位的任何机器上都能正确地运行。

开始作答 官方答案(已验证)

A.要进行左移的数大于或等于x的宽度。
B.修改如下:

int bad_int_size_is_32() {
	int set_msb = 1 << 31;
	int beyond_msb = set_msb << 1;
	return set_msb && !beyond_msb;
}

C.关键就是在不能一次左移达到这个x的位数,不符合标准。

#include <stdio.h>
#include <assert.h>

/* The following code does not run properly on some machines */
int int_size_is_32() {
	int set_msb = 1 << 31;
	int beyond_msb = set_msb << 1;
	return set_msb && !beyond_msb;
}

int int_size_is_32_for_16bit() {
	int set_msb = 1 << 15 << 15 << 1;
	int beyond_msb = set_msb << 1;
	return set_msb && !beyond_msb;
}

int main(int argc, char* argv[]) {
	assert(int_size_is_32());
	assert(int_size_is_32_for_16bit());
	return 0;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在MATLAB中,可以使用以下代码将一个3x3的方向余弦矩阵转换为四元数: ```matlab function q = dcm2quat(dcm) % DCM2QUAT Convert direction cosine matrix to quaternion % q = DCM2QUAT(dcm) converts a 3x3 direction cosine matrix (DCM) to a 1x4 % quaternion. The DCM input represents the rotation from frame 2 to frame % 1, where frame 1 is the global frame and frame 2 is the rotated frame. % % Reference: Eq. 2.65 and 2.67, "Quaternion Kinematics for the Error-State % Kalman Filter", Joan Solà, 2017. % Check input size if any(size(dcm) ~= [3 3]) error('Input matrix must be 3x3.') end % Compute quaternion elements q0 = 0.5 * sqrt(1 + dcm(1,1) + dcm(2,2) + dcm(3,3)); q1 = (dcm(3,2) - dcm(2,3)) / (4*q0); q2 = (dcm(1,3) - dcm(3,1)) / (4*q0); q3 = (dcm(2,1) - dcm(1,2)) / (4*q0); % Return quaternion as a row vector q = [q0 q1 q2 q3]; ``` 然后,可以使用以下代码将四元数单位化: ```matlab function q_unit = quatnormalize(q) % QUATNORMALIZE Normalize quaternion % q_unit = QUATNORMALIZE(q) normalizes the quaternion q to have unit % magnitude. % Check input size if any(size(q) ~= [1 4]) error('Input vector must be 1x4.') end % Compute quaternion magnitude q_mag = norm(q); % Normalize quaternion q_unit = q / q_mag; ``` 现在,您可以将这两个函数组合在一起,使用以下代码将方向余弦矩阵转换为单位四元数: ```matlab % Example DCM dcm = [0.7071 -0.7071 0; 0.7071 0.7071 0; 0 0 1]; % Convert DCM to quaternion q = dcm2quat(dcm); % Normalize quaternion q_unit = quatnormalize(q); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

榆钱不知秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值