lisp program for hexadecimal to decimal - lisp

how to write a lisp program to convert given hexadecimal number into decimal. can somebody give me a clue.
thank you

I'm assuming its a homework problem so i'll give you a hint in the right direction.
Here is how to convert decimal to binary ->
Lets say you start with the number 9 in binary its 1001.
Start of by dividing 9 by 2. You get 4 with remainder 1. Save the remainder.
Now divide that 4 by 2 again, you get 2 with remainder 0. Save the remainder.
Divide that 2 again by 2, you get 1 with remainder 0. Save the remainder.
Divide that 1 by 2 and finally you get 0 with reaminder 1. Save the remainder.
If you read the saved remainders backwards you get 1001! The binary number you've been looking for. Best to push the remainders on the stack and pop them back out, that way they'll come out backwards.

It's already provided by Common Lisp.
The input is the string for the hex integer.
Then you parse the integer with radix 16
the result is the number
if you write the number with base 10 to an output stream, then you can get the number as a string in base 10

Related

How can I print the ascii value of an input in Brainfuck?

What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input())). What I'm thinking is that once I get the input with the , command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a. The , command will store the ascii value of a in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.
What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

Converting a floating point number to string

I want to convert a floating point number to string.
f=0.0000
str=num2str(f,4);
This give me this-
str=
0
But I wanted to keep the result till 4 decimal points.I understand using num2str(f,4) results in precision of maximum 4 significant digits after decimal.It is not equal to 4 but maximum and that is why I get this answer.I want to know is there any way to convert this number to string with exactly 4 significant digits after decimal point?
Try using sprintf instead:
str = sprintf('%.4f', f)

reading a number with upto 10 decimal point precision from txt file

I have numbers with upto 10 decimal points stored in a text file.
I read it in MATLAB and then do str2double on the number but i get only upto 4 decimal points. What should I do to get all the values after decimal.
Forexample:
str2double('-122.345464646')
ans =
-122.3455
but I need the entire number
Thanks
Please follow the below steps:
Go to preference
List item
Go to Command Window Option
Then change the numeric format to long g
Alternatively, type long g in command window

Random Number in Octave

I need to generate a random number that is between .0000001 and 1, I have been using rand(1) but this only gives me 4 decimal points, is there any other way to do this generation?
Thanks!
From the Octave docs:
By default, Octave displays 5 significant digits in a human readable form (option ‘short’ paired with ‘loose’ format for matrices).
So it's probably an issue with the way you're printing the value rather than the value itself.
That same page shows the other output formats in addition to short, the one you may want to look in to is long, giving 15 significant digits.
And there is also the output_precision which can be set as per here:
old_val = output_precision (7)
disp (whatever)
old_val = output_precision (old_val)
Set the output_precision to 7 and it should be ok :)
Setting the output precision won't help though because the number can still be less than .0000001 in theory but you will only be displaying the first 7 digits. The simplest way is:
req=0;
while (req<.0000001)
req=rand(1);
end
It is possible that this could get you stuck in a loop but it will produce the right number. To display all the decimals you can also use the following command:
format long
This will show you 15 decimal places. To switch back go:
formay short

How to store more than 4 decimal places in an array in MATLAB

I want to store 6 decimal digits into an array, but when I store it into array it only stores up to 4 decimal digits instead of 6. How can I store up to 6 digits into an array?
For example, if
e=0.059995;
W(l,i)=e;
but W(l,i) gives me the result as 4 decimal places
disp(W(l,i))
0.0600
How can I store 6 decimal digits into an array, i.e when I print the array it prints
6 decimal places?
disp(W(l,i))
0.059995
Can anyone help me?
Matlab on default settings stores up to 15 digits. It only your display format. Have a look at the format command.
Or just type at the Matlab command prompt:
format long
If you know you have only 6 digits you can use
sprintf('%0.6f', W(l,i))
instead of disp