2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

(その5).構造体で遊ぶ。(浮動小数点を含む構造体)(最終章)

Last updated at Posted at 2025-01-21

x 過去ログを見よ!!!
x M5NanoC6での予定は、未定

目的
浮動小数点数も含む時

いろいろ
浮動小数点数「警察」が いるらしい
特に、計算誤差や
比較には、うるさいらしい
= は、 ほぼ、禁止らしい
ゲームとかドローンとかは、なぜかアナログデータ(ADCした)
そのまま(浮動小数点)で送っているような気がする?



#include <iostream>
#include <string.h>  //memcpy
using namespace std;
int main(void){
    // Your code here!
    printf("START\n");
    
    unsigned char ac[256+1];
    
    struct {
        float x;
        float y;
    } X_Y;
    
    X_Y.x = 2.5 * 2.5 * 2.5; //15.625
    X_Y.y = 2.5 * 2.5 * 2.5 * 2.5; //39.0625
    
    unsigned char *c;
    c = (unsigned char *)(&X_Y);

    memcpy(ac, c, (sizeof(float)) * 2); 

    printf("[%x][%x][%x][%x]\n",ac[0],ac[1],ac[2],ac[3]);
    printf("[%x][%x][%x][%x]\n",ac[4],ac[5],ac[6],ac[7]);

    unsigned char *p;
    p = (unsigned char *)(&X_Y);
    
    memcpy(p, ac, (sizeof(float)) * 2); 

    printf("\n");
    printf("[%f],[%f]\n", X_Y.x, X_Y.y);
    
}//main




START
[0][0][7a][41]
[0][40][1c][42]

[15.625000],[39.062500]

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?