5873 views|13 replies

3407

Posts

0

Resources
The OP

Operations on different variable types [Copy link]

This post was last edited by ljj3166 on 2015-11-17 13:08 I was DIYing a DDS here a few days ago and encountered a very interesting problem. I wrote it here for everyone to see
  1. unsigned int FREQ_MHz=0,FREQ_kHz=0,FREQ_Hz=0; long unsigned int FREQ=1000;
复制代码
Three unsigned integer variables FREQ_MHz, FREQ_kHz and FREQ_Hz and an unsigned long variable FREQ are defined. Four variables are used for calculation
  1. FREQ = FREQ_MHz*1000000+FREQ_kHz*1000+FREQ_Hz;
复制代码
In the above calculation, if the value of FREQ_kHz variable exceeds 50, the calculation of FREQ_kHz*1000 will fail. For example, FREQ_MHz=3, FREQ_kHz=66, FREQ_Hz=10 The result of the operation is FREQ = 3042610. I guess the problem is caused by the wrong variable type, so I changed it to
  1. FREQ = FREQ_MHz*1000000+(long)FREQ_kHz*1000+FREQ_Hz;
复制代码
Everything works fine, so the question is why the calculation result is correct without forced conversion of FREQ_MHz and FREQ_Hz? Experts, please come out and add some information. The compilation environment is CCS6
This post is from Programming Basics

Latest reply

16-bit compiler  Details Published on 2016-2-21 20:10
Personal signature

So TM what......?


8199

Posts

2

Resources
推荐
Yes, there is one that improves the entire expression to be expressed in the longest type. It should be OK. = M*100000UL + K*1000UL + H*1UL should be written more beautifully, more uniformly, and less overbearingly and forcefully. . .
This post is from Programming Basics

Comments

M*100000 is upgraded to long, but k*1000 is still hovering in Uint16_t...  Details Published on 2015-11-17 14:48

3407

Posts

0

Resources
3
freebsder posted on 2015-11-17 14:39 Yes, there is a way to promote the whole expression to the longest type. It should be OK. = M*100000UL + K*1000UL + H*1UL ...
M*100000 is promoted to long, but k*1000 is still hovering in Uint16_t...
This post is from Programming Basics
Personal signature

So TM what......?


8199

Posts

2

Resources
4
The entire expression is evaluated as the longest type of the entire statement.
This post is from Programming Basics

6376

Posts

17

Resources
5

Why is the result correct if FREQ_MHz and FREQ_Hz are not converted? One of the variables in the addition is a long unsigned int, and the others will be converted accordingly. Try converting the other two variables, and see if the result is the same.
This post is from Programming Basics
Personal signaturetraining

6376

Posts

17

Resources
6
long unsigned int + unsigned int, first convert the unsigned int to long unsigned int, then perform the calculation
This post is from Programming Basics
Personal signaturetraining

6376

Posts

17

Resources
7
It's strange why the result is 3042610
This post is from Programming Basics

Comments

The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.  Details Published on 2015-11-17 17:15
Personal signaturetraining

4170

Posts

8

Resources
8
Your result is very strange. I ran it with CB. Here is my result:
This post is from Programming Basics

Comments

The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.  Details Published on 2015-11-17 17:18
The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.  Details Published on 2015-11-17 17:15

3407

Posts

0

Resources
9


The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.
This post is from Programming Basics

3407

Posts

0

Resources
10


The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.
This post is from Programming Basics

Comments

Overflow? How can such a small number overflow?  Details Published on 2015-11-17 18:14

4170

Posts

8

Resources
11
huaiqiao posted on 2015-11-17 17:11 Your result is very strange. I ran it with CB. Please take a look at the result:
OK, please post the picture and we can take a look. O(∩_∩)O Haha~
This post is from Programming Basics

6376

Posts

17

Resources
12
ljj3166 posted on 2015-11-17 17:15 The calculation result of my code should be the result of unsigned integer overflow. I will post the simulation later.
Overflow? How can such a small number overflow?
This post is from Programming Basics
Personal signaturetraining

7818

Posts

43

Resources
13
Just tell me whether your MCU is msp430
This post is from Programming Basics
Personal signature

强者为尊,弱者,死无葬身之地


6016

Posts

136

Resources
14
16-bit compiler
This post is from Programming Basics

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews

    Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list