0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

STM32G031で二進固定小数点で遊ぶ2(四則演算)

Last updated at Posted at 2022-06-09

x 動作確認済み2022/6/10 7:42

x OBでGPIOがわかる人むけ

目的 
適当にごもごもするから四則演算をまともに
した時は、ないが今後の為

二進固定小数点 2ビット 0.25 ←→ 1/4 の話

二進固定小数点の時は、とりあえず4倍するんや!!
そうすると精度が4倍になり0,.25,.5,.75がねじ込めるるんや!!

足し算と引き算は、そのまま

A + B = C

A - B = C

1.25 + 1.50 = 2.75

2.75 - 1.50 = 1.25

足し算

1.25 + 1.50 = 2.75

1.25 → (1 * 4)+(1) = 5(101B)
1.50 → (1 * 4)+(2) = 6(110B)

101B + 110B = 11(1011B)

2.75 → (2 * 4)+(3) = 11(1011B)

引き算

2.75 - 1.50 = 1.25

2.75 → (2 * 4)+(3) = 11(1011B)
1.50 → (1 * 4)+(2) = 6 (110B)

1011B - 110B = 5(101B)

1.25 → (1 * 4)+(1) = 5(101B)

掛け算は、小数点分右にシフト

小数点N

A * B = C

C >> N

2.0 * 1.5 = 3.0

2.0 → (2 * 4) + (0) = 8
1.5 → (1 * 4) + (2) = 6

8 * 6 = 48

48 /2 = 24
24 /2 = 12

12 → ( 3 * 4 )+(0)

3.0

割り算

割り算は、小数点分左にシフトして割る

小数点N

( A << N ) / B = C

3.0 / 1.5 = 2.0

3.0 → (3 * 4) + (0) = 12
1.5 → (1 * 4) + (2) = 6

(12 * 4) / 6 = 8
48 / 6 = 8

8 → (2 * 4) + (0)

2.0


#include <stdio.h>
int main(void){
    
    
    int a,b,c;
    char *f4[]={".0",".25",".5",".75"};
    
    
    //1.25 + 1.50 = 2.75
    
    a = (1<<2)+(1); //1.25
    b = (1<<2)+(2); //1.5
    c = a+b;
    printf("%d",c>>2);
    printf("%s\r\n",f4[c & 0x03]);
    
    
    //2.75 - 1.50 = 1.25
    
    a = (2<<2)+(3); //2.75
    b = (1<<2)+(2); //1.25
    c = a-b;
    printf("%d",c>>2);
    printf("%s\r\n",f4[c & 0x03]);
    
    
    //2.0 * 1.5 = 3.0
    
    a=(2<<2)+(0); //2.0
    b=(1<<2)+(2); //1.5
    c = a*b;
    c = c>>2;
    printf("%d",c>>2);
    printf("%s\r\n",f4[c & 0x03]);
    
    
    //3.0 / 1.5 = 2.0

    a = (3<<2)+(0); //3.0
    b = (1<<2)+(2); //1.5
    c=(a<<2)/b;
    printf("%d",c>>2);
    printf("%s\r\n",f4[c & 0x03]);
    
}




2.75
1.25
3.0
2.0

o_con530.jpg




//SER_f4_4_031_1


#include <Arduino.h>
#include <HardwareSerial.h>


//初期化
void setup() {

  delay(3000); //not delete

  Serial.setTx(PA2_ALT1);
  Serial.setHalfDuplex();
  Serial.begin(9600);


}//setup

int a,b,c;
char *f4[]={".0",".25",".5",".75"};

//メインループ
void loop() {

    //1.25 + 1.50 = 2.75

    Serial.println("1.25 + 1.50");
    a = (1<<2)+(1); //1.25
    b = (1<<2)+(2); //1.5
    c = a+b;
    Serial.print(c>>2);
    Serial.println(f4[c & 0x03]);
    
    
    //2.75 - 1.50 = 1.25

    Serial.println("2.75 - 1.50");
    a = (2<<2)+(3); //2.75
    b = (1<<2)+(2); //1.25
    c = a-b;
    Serial.print(c>>2);
    Serial.println(f4[c & 0x03]);
    
    
    //2.0 * 1.5 = 3.0

    Serial.println("2.0 * 1.5");
    a=(2<<2)+(0); //2.0
    b=(1<<2)+(2); //1.5
    c = a*b;
    c = c>>2;
    Serial.print(c>>2);
    Serial.println(f4[c & 0x03]);
    
    
    //3.0 / 1.5 = 2.0

    Serial.println("3.0 / 1.5");
    a = (3<<2)+(0); //3.0
    b = (1<<2)+(2); //1.5
    c=(a<<2)/b;
    Serial.print(c>>2);
    Serial.println(f4[c & 0x03]);


  delay(4000);

}//loop





0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?