Hey all! I was wondering if there was a way to better use Print(U)Int( in ICE so that it will print out numbers with differing amount of digits (Ex. PrintUInt(82,4) prints 0082). If there is some way of having a method that would find the digit count or some other way to print similar to the Float option. I know the mod(10) /10 but I'm not sure how to do this so it works with decimals and is in ICE. Thanks to any who can help or provide tips!
What I've done before is created a label that returns the number of characters in a given input. I don't know if this is the best way to do things.

Here's an example. I want to print out 82 (this could also be a variable).

Code:
``` 82->A //'A' is the input Call RCHAR //call the label to return the number of chars PrintInt(A,CHARS) //Var CHARS is the number of characters in 'A' and is returned by the label we called Lbl RCHAR 1->CHARS If A>9 2->CHARS End If A>99 3->CHARS End Return ```
That is a very bad way to do it, and it reminds me of this meme from reddit.

Ideally, you would do this with a log(), but ICE doesn't have that, but I would suggest to just use the asm call.
Unfortunately, I don't know how ICE stores and uses variables, PT_ could surely help out here, so I will pretend the number you are trying to find the length of is in Ans, because there are calls for that too.

Code:
```call _RclAns call _LogX call _Int call _StoAns ret ```

Which in hex, looks like this:

Code:
`CD500F02CD100202CD040202CD300F02C9`

Then just use the Asm() command to insert it into your ICE program like this

Code:
`Asm(CD500F02CD100202CD040202CD300F02C9)`

However, this assumes that the value is stored in Ans, which is not the case with ICE variables, so getting the values of the variables and setting them won't be as easy as just RclAns and StoAns.
mr womp womp wrote:
That is a very bad way to do it

Your way is much worse. In fact, dankcalculatorbro's is probably the best.
dankcalculatorbro wrote:

Code:
``` 82->A //'A' is the input Call RCHAR //call the label to return the number of chars PrintInt(A,CHARS) //Var CHARS is the number of characters in 'A' and is returned by the label we called Lbl RCHAR 1->CHARS If A>9 2->CHARS End If A>99 3->CHARS End Return ```

I would suggest this way as well. I implemented a similar concept in my StockSim program. Until a feature to auto-detect the number of digits is added, this will remain the best way to accomplish this.

(Note to self, use this code instead and optimize)
MateoConLechuga wrote:
mr womp womp wrote:
That is a very bad way to do it

Your way is much worse. In fact, dankcalculatorbro's is probably the best.

The difference is that my way is not hard coded, and will work on any given input. Granted, the speed is probably what made you sad, but I would prefer having fault tolerance over speed (in a case where there really is no noticeable difference either way). Also, if PT_ wanted to, he could easily make an optimized Log function, since you only need the truncation of the result, which could make this much faster.
This looks like a better and simpler way to do it to me:

Code:
```Input B B->A 1->DIGITS If A≥10 Repeat A<10 A/10->A DIGITS+1->DIGITS End End PrintInt(B,DIGITS ```

Might requires some optimization though.
try poking PT_ for toString in ICE
(and maybe a few other tokens, like eval or expr, whichever one it is that takes a string and outputs a number... I forget...)

***SM84CE hides
I think I have the most optimized version:

Step 2: Write moar optimized code:

Code:
```PrintInt(B,1 ```

Step 3: Enjoy Be sure to thank Runer for this handy optimization and code!
Ooh. This will reduce alot off of my previous programs!
SM84CE wrote:
try poking PT_ for toString in ICE
(and maybe a few other tokens, like eval or expr, whichever one it is that takes a string and outputs a number... I forget...)

***SM84CE hides

Although not released yet, PT_ has already implemented Log() and toString()
Proof:

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

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