Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Val() not so valuable

Author
Message
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 18th Sep 2014 18:24
I have read related posts but I am hoping that there might be a simple answer or fix for accurate storage of an integer number string as an integer. I am storing a lot of them and was thinking that storing them as integers might be more efficient, but I may be wrong on that too. Anyway, here is some simple code illustrating my problem.



Any insights would be appreciated.

Le Verdier
14
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 18th Sep 2014 18:47 Edited at: 18th Sep 2014 18:52
This because Val() is for floating numbers!
and these have only 24(23+1) bits of precision..
It's why some bits are cancelled with some large integer numbers
But there is an IntVal function in IanM's Matrix1DLLs !!

All hail the new flesh
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 18th Sep 2014 22:46
So there is! I use his stuff all the time and hadn't noticed it. Thanks so much for the tip. Is Mold amazing or what?

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 19th Sep 2014 00:06
Btw, just a tip that helped me a lot: If you're ever looking for a command or function, the first thing you should think is "hmm, is it in Matrix1 Utils?"

I like offending people. People who get offended should be offended. -- Linux Torvalds
freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 19th Sep 2014 04:10
I know. I've been away from the forums for awhile and forgot my usual pattern, which is:

1. Encounter insurmountable coding problem that threatens to derail the entire project.

2. Go to Matrix Utilities

3. Find solution that Ian anticipated 10 years ago.

Or put another way, could DBP be taken seriously without Matrix1 Utilities? My code is Moldy and I'm proud of it! Further, I think he should port them to every computer language known to man.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Sep 2014 12:54
Quote: "My code is Moldy and I'm proud of it!"


Same here. His commands are such second nature to me that I often forget to warn people that I've used them in my demos.



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Sep 2014 02:16
Quote: "
1. Encounter insurmountable coding problem that threatens to derail the entire project.
"


Well, while you probably don't mean that completely seriously, the quickest solution in this case would probably have been this:



Doesn't detect negative values, but works fine for positve integers. The matrix1 implementation is most certainly faster and more capable and hence the better choice. I just mean... one should not give up on a project if all you need is a working val() function.

BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 21st Sep 2014 18:44 Edited at: 21st Sep 2014 18:44
To answer your other statement in the OP, storing integers is more memory-efficient in your case. An integer stores basically any number in 4 bytes. A string takes one byte per character plus one terminating character. So strings are more memory-efficient than integers only if your numbers are less than 3 digits (and maybe not even then, since your computer can't allocate things smaller than a certain size and will probably use 4 bytes anyway)

ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Sep 2014 23:23 Edited at: 21st Sep 2014 23:25
Quote: "So strings are more memory-efficient than integers only if your numbers are less than 3 digits (and maybe not even then, since your computer can't allocate things smaller than a certain size and will probably use 4 bytes anyway)"


And even then, using words or bytes would be even more efficient. I'm not sure how strings are implemented in DBP, but there either has to be an additional value (which again would probably be a word or dword) storing its length, or strings are 0-terminated, i.e. there's one more byte that's not part of the actual string that simply has the meaning "this is where the string is over".

So, if it's just about storing numbers, integers are most certainly always preferable to strings. The only case I know of where converting integers to string and vice versa is for GUI related stuff, e.g. if the user has to insert a number or if a game's interface shows a number in textual form.

Edit: Also, you might want to use strings or some other system when handling numbers that exceed the 4 byte limitations - but in that case, there's no point in converting the number to int, so I guess that's not the case here.

freight hopper
22
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Just beyond the Dunsmuir yard limits
Posted: 24th Sep 2014 04:35
Thanks Big Mac and Shellfish. There may come a time when I am outside the Matrix1 safety net and options are always good.

Login to post a reply

Server time is: 2026-07-18 07:32:01
Your offset time is: 2026-07-18 07:32:01