Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How do floats work?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Jackey23
How do I cheat?
Reputation: 0

Joined: 17 Sep 2019
Posts: 5

PostPosted: Sat Oct 05, 2019 11:32 am    Post subject: How do floats work? Reply with quote

For example, in Hotline Miami 2 WR the float 1.875 is 1 and the number 2 is just 2 and 3 is 2.125. How does that work?
Back to top
View user's profile Send private message
Meas
Newbie cheater
Reputation: 0

Joined: 31 Oct 2015
Posts: 18

PostPosted: Mon Oct 07, 2019 6:43 am    Post subject: Reply with quote

Can you give me details on which value you mean? Maybe post some screenshots.

Integer 2 is the same as float 2.0. They shouldn't vary unless the game's code is scaling the values.
Back to top
View user's profile Send private message
Jackey23
How do I cheat?
Reputation: 0

Joined: 17 Sep 2019
Posts: 5

PostPosted: Sat Oct 12, 2019 10:10 am    Post subject: Reply with quote

Ok, Im just gonna say, in hotline miami 2 wrong nubmer there is a combo system and when I got a combo of 1 the value in cheat engine is 1.875 and combo of 2 is 2 and 3 is 2.125 and 45 is 3.1015625, that makes on sense
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Sat Oct 12, 2019 11:47 am    Post subject: Reply with quote

This looks like Java type conversions. Seen this before on a few games where values are stored as one thing and converted back to another for use. Java implements their floating point math and types via:
https://en.wikipedia.org/wiki/IEEE_754

In the JDK you can see the source as to how these are converted here:
https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/native/java/lang/Double.c#L52
https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/native/java/lang/Float.c#L35

You can recreate these calls in C++ to perform the same conversion/math and get the results you are seeing like this:

Code:

#include <Windows.h>
#include <string>

/**
 * Java function to convert a double to int64_t. (IEEE 754)
 */
int64_t Java_DoubleToLongBits(double d)
{
    union
    {
        int64_t l;
        double d;
    } u;

    u.d = (double)d;
    return u.l;
}

/**
 * Java function to convert an int32_t to float. (IEEE 754)
 */
float Java_IntBitsToFloat(int32_t n)
{
    union
    {
        int32_t i;
        float f;
    } u;

    u.i = n;
    return (float)u.f;
}

/**
 * Application entry point.
 */
int32_t __cdecl main(int32_t argc, char* argv[])
{
    for (auto x = 0; x < 50; x++)
    {
        // Convert the int to Java long bits..
        const auto bits = Java_DoubleToLongBits((double)x);

        // Convert the long bits to a float.. (Shifting from 64bit to 32bit sized value.)
        const auto f = Java_IntBitsToFloat(bits >> 32);

        // Print the result..
        printf_s("%d - %f\r\n", x, f);
    }

    return 0;
}


Which gives:
Code:

0 - 0.000000
1 - 1.875000
2 - 2.000000
3 - 2.125000
4 - 2.250000
5 - 2.312500
6 - 2.375000
7 - 2.437500
8 - 2.500000
9 - 2.531250
10 - 2.562500
11 - 2.593750
12 - 2.625000
13 - 2.656250
14 - 2.687500
15 - 2.718750
16 - 2.750000
17 - 2.765625
18 - 2.781250
19 - 2.796875
20 - 2.812500
21 - 2.828125
22 - 2.843750
23 - 2.859375
24 - 2.875000
25 - 2.890625
26 - 2.906250
27 - 2.921875
28 - 2.937500
29 - 2.953125
30 - 2.968750
31 - 2.984375
32 - 3.000000
33 - 3.007813
34 - 3.015625
35 - 3.023438
36 - 3.031250
37 - 3.039063
38 - 3.046875
39 - 3.054688
40 - 3.062500
41 - 3.070313
42 - 3.078125
43 - 3.085938
44 - 3.093750
45 - 3.101563
46 - 3.109375
47 - 3.117188
48 - 3.125000
49 - 3.132813

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Jackey23
How do I cheat?
Reputation: 0

Joined: 17 Sep 2019
Posts: 5

PostPosted: Sat Oct 19, 2019 12:45 pm    Post subject: Reply with quote

So what could I change in the code that you wrote to get a float of a number I want, because I don't understand any of the code you wrote
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Sat Oct 19, 2019 6:59 pm    Post subject: Reply with quote

Code:

// Change x's value for what you want to convert..
const auto x = 1;

// Convert the int to Java long bits..
const auto bits = Java_DoubleToLongBits((double)x);

// Convert the long bits to a float.. (Shifting from 64bit to 32bit sized value.)
const auto f = Java_IntBitsToFloat(bits >> 32);

// Print the result..
printf_s("%d - %f\r\n", x, f);


You can take this chunk, change x's value at the top to have it spit out the value you want to convert.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Jackey23
How do I cheat?
Reputation: 0

Joined: 17 Sep 2019
Posts: 5

PostPosted: Sun Oct 20, 2019 4:01 am    Post subject: Reply with quote

Thanks a lot
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites