0 Daumen
280 Aufrufe

Frage:

Ist es grundsätzlich möglich, von einem int32 zu einem float32 zu casten ohne Informationsverlust?

Ich würde sagen, dass int32->float32 ein gültiger Cast wäre. Andersrum wäre wohl ein bisschen kritischer,

also von float64- -> int32

Gruß

Avatar von

Dazu gibt es union:


union einheit

{
float messwert;
unsigned char mwertbytes[4];

unsigned short mwertshort[2];  unsigned int integerwert;
};


Bei meinem tumbleweed sind die Bytes bis zum Typ int nicht codiert. Bei längeren Datentypen schon.Kannst du aber testen ob der Speicher codiert ist oder nicht:


/*
* Copyright (C) 2017 Josef Wismeth <josef.wismeth@t-online.de>
*
* abstest is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* abstest is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>

double dabs(double wert)
{
int vzbyte;
union test
{
double absolutwert;
unsigned char a[8];
}tst;

if ((sizeof(double)) == 8)
{
tst.absolutwert = wert;
vzbyte = tst.a[7] & 128;
if(vzbyte != 0) tst.a[7] &= 127;
}
else tst.absolutwert *= -1;
return tst.absolutwert;
}


int main()
{
double plus = -25, abswert;

abswert = plus;

printf("Vorzeichenbit-Test\n");
printf("Länge der Datentypen.\n");
printf("Datentyp char.......: %ld\n", sizeof(char));
printf("Datentyp short......: %ld\n", sizeof(short));
printf("Datentyp int........: %ld\n", sizeof(int));
printf("Datentyp long.......: %ld\n", sizeof(long));
printf("Datentyp double.....: %ld\n", sizeof(double));
printf("Datentyp long double: %ld\n", sizeof(long double));
printf("dabs-Test:\n");
printf("Pluswert.................: %lf\n", plus);
printf("abswert alt..............: %lf\n", abswert);

abswert = dabs(plus);

printf("abswert neu..............: %lf\n", abswert);

return (0);
}
Wenn nach dem Vorzeichenwechsel nur das Vorzeichen sich ändert aber nicht der Wert danach, dann klappt das mit union.

2 Antworten

0 Daumen

So was kann nützlich sein wenn man die float-Daten nicht als Text, sondern Byteweise in eine Datei schreiben will oder zur Chiffrierung von Nachrichten. da der Typ float im Normalfall bis zu 6 Stellen hinter dem Komma fehlerfrei verarbeitet, kann man die zu codierenden Buchstaben getarnt als Nachkommastellen in float-Variablen packen und so abspeichern:

#include <iostream>
#include <cmath>
#include <iomanip>

int Getvk(double wert)
{
return (int)wert; 
}

double mul1k(double wert)
{
return wert * 1000; 
}

void getletters(double wert[], char test[])
{
int i, j, k;
double nk, vk, dummya;

k = 0;
for(j = 0; j <= 3; j++)
{
nk = std::modf(wert[j], &vk);


for(i = 0; i < 3; i++)
  {
  dummya = mul1k(nk);
  vk = Getvk(dummya);
 
  if(vk > 13)
    {
    test[k] = vk;
    test[k + 1] = '\0';
    k++;
    }
 
 
 
  nk = std::modf(dummya, &vk);
  }

}
}


int main(int argc, char **argv)
{
double wert[4] ={12.072097108001,
              28.108111032001,
              33.087101108001,
              33.116013000001};

char test[50] = {0};

getletters(wert, test);

std::cout << "\nTeststring: " << test << std::endl<< std::endl;

return 0;
}


So kann man zum Beispiel den wahren Urheber einer Funktion so absichern. Wird versucht, den Namen zu ändern um gegen das Urheberrecht zu verstoßen, liefert eine so abgesicherte Funktion falsche Resultate.

Ähnlich haben es in Mittelalter Gelehrte gemacht, um so sicher zu stellen das Sie die Entdecker waren, mit einem Anagram. Das ist nur eine moderne Form eines Anagram. Wenn man noch mit XOR-Verschlüsselung arbeitet, ist das ziemlich schwer sich so quasi mit frenden Federn zu schmücken.

Avatar von
0 Daumen
Ist es grundsätzlich möglich, von einem int32 zu einem float32 zu casten ohne Informationsverlust?

Nein

#include <limits.h>
#include <stdio.h>
int main() {
  int i = INT_MAX-63;
  float f = i;
  printf("int:\t%d (%ld bits)\nfloat:\t%f (%ld bits)\n",
    i, 8*sizeof(int), f, 8*sizeof(float));
}

Ausgabe:

int:    2147483584 (32 bits)
float: 2147483648.000000 (32 bits)
Ich würde sagen, dass int32->float32 ein gültiger Cast wäre.

"Gültig" heißt nicht, dass es dabei zu keinem Informationsverlust kommt.

Avatar von 5,6 k

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community