Spoiler: Precise fixed point string format (Open)Normally, when printing (or converting into a string) a fixed point number with the f: format specifier, a lot of precision is lost.
This happens because internally Zandronum (and really just G/ZDoom in general) converts the fixed point number to a floating point number, losing precision in the process. This precision loss becomes more and more extreme the larger the number.
This function does a more accurate conversion that loses much less precision, and has an adjustable amount of decimal places. If you specify 0 for decPlaces, you'll get 4, which is the max, and default amount of decimal places.
Some testing results:Code: Select all
function str PreciseFixedFmt (int n, int decPlaces) { str retStr; int integerPart = n >> 16; int fractPart = (n & 65535) * 10000 / 65536; if(fractPart < 1) retStr = StrParam(s:"0000"); else if(fractPart < 10) retStr = StrParam(s:"000", i:fractPart); else if(fractPart < 100) retStr = StrParam(s:"00", i:fractPart); else if(fractPart < 1000) retStr = StrParam(s:"0", i:fractPart); else retStr = StrParam(i:fractPart); if( (decPlaces > 0) && (decPlaces < 4) ) { retStr = StrLeft(retStr, decPlaces); } return StrParam(i:integerPart, c:'.', s:retStr); }
Code: Select all
Log(s:"== Normal fixed print =="); Log(f:10.5); Log(f:0.25); Log(f:5.001); Log(f:32767.8523); Log(s:"== PreciseFixedFmt =="); Log(s:PreciseFixedFmt(10.5, 0)); Log(s:PreciseFixedFmt(0.25, 0)); Log(s:PreciseFixedFmt(5.001, 0)); Log(s:PreciseFixedFmt(32767.8523, 0));
[ACS] Generally useful code snippets
[ACS] Generally useful code snippets
I'll be using this thread to post ACS code snippets that aren't quite large enough or too specific for a library but are still fairly useful.
Spoiler: Precise fixed point string format (Open)
One thing I have asked from the Lord, that I shall seek: That I may dwell in the house of the Lord all the days of my life, to behold the beauty of the Lord and to meditate in His temple. (Psalm 27:4, NASB)
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.
-
- Forum Regular
- Posts: 256
- Joined: Wed Sep 09, 2015 2:21 pm
- Location: In home, sweet Home
- Clan Tag: <skr>
Re: [ACS] Generally useful code snippets
Some other few codes that I found, and could be useful for someone.
Spoiler: StrTrim: removes all blank spaces between the start and the end of a string. Keeping the inner spaces intact. (Open)Also, this code made by Trillster
Spoiler: GiveActorReplacedInventory: Simply gives the item that it has been already repleaced through decorate (Open)
Last edited by Samuzero15tlh on Thu Aug 31, 2023 2:41 pm, edited 1 time in total.
Everyone wants happiness without pain, but you cant have a rainbow without a rain.
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Samuzero15tlh
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Spoiler: My Other zandro stuff! (Open)
Samuzero15tlh
Re: [ACS] Generally useful code snippets
It would be more convenient if the StrTrim function didn't require ACSUtils just for StrSlice, especially considering how trivial a function that is.
Would instead be
And the other StrSlice occurence doesn't even require any changes, it'll just work if changed to StrMid.
Code: Select all
newstring = StrSlice(string, leftchar, len);
Code: Select all
newstring = StrMid(string, leftchar, len - leftchar);
One thing I have asked from the Lord, that I shall seek: That I may dwell in the house of the Lord all the days of my life, to behold the beauty of the Lord and to meditate in His temple. (Psalm 27:4, NASB)
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.
-
- Forum Regular
- Posts: 256
- Joined: Wed Sep 09, 2015 2:21 pm
- Location: In home, sweet Home
- Clan Tag: <skr>
Re: [ACS] Generally useful code snippets
Alright, updated, StrTrim now does'nt really need ACS Utils to work out, but still, I kept the old code just in case.
Everyone wants happiness without pain, but you cant have a rainbow without a rain.
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Samuzero15tlh
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Spoiler: My Other zandro stuff! (Open)
Samuzero15tlh
-
- Forum Regular
- Posts: 256
- Joined: Wed Sep 09, 2015 2:21 pm
- Location: In home, sweet Home
- Clan Tag: <skr>
Re: [ACS] Generally useful code snippets
'nother snippet!
Spoiler: StrCharCount, Counts how many characters are inside a given string, useful for counting arguments in Lump Reading. (Open)
Everyone wants happiness without pain, but you cant have a rainbow without a rain.
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Samuzero15tlh
I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.
Spoiler: My Other zandro stuff! (Open)
Samuzero15tlh