How to check if user input fits in variable? - validation

I'm trying to write a simple program to calculate a function with Fortran95/03 which needs as input a number(x) and gets as output a number(y).
The user input is a real :: input and the read call looks like
read (*,*, iostat=stat) input
if(stat > 0) then
print *, "False input, use numbers!"
The iostat helps me to check if the input was a number or a letter.
My problem is that if I enter a very big number, like 1000000000000, the program crashes with the error message "bufferoverflow". I know that I can make the real bigger than a 4 byte variable, but I also can make the input number bigger, so this does not solve the problem.
The main question is, is it possible to prevent the program crashing because of user input?

Checking the values of the user's input is a very basic technique which must be employed in all kinds of software which interacts with someone else than just the author. It is used in all programming languages.
You can just use a simple condition
if (input > input_max) then
print *, "Input value too large, try again"
cycle ! or return stop or set some flag or whatever
end if
Don't forget the value may be also too small!
It is important to understand where does the crash come from. It certainly does not come from just from inputting a large number but using the number in a bad way, for example, allocating an array which is too large or by making a calculation which triggers a floating point exception.

Read the input as a string, then validate the string input, then use an internal read to convert the validated string into a REAL.
There are many aspects of processor dependent behaviour to input and output, as a general principle if you want robustness then you need to do much of the leg work yourself. For example, if malformed input for a real is provided, then there is no requirement that a processor identify that as an error condition and return a non-zero IOSTAT code.
List directed input offers further challenges, in that it has a number of surprising features that may trip you and your users up.

Related

How to process sensor data in LabVIEW? Every value is 255

I'm trying to read data from the Yost Labs 3-Space Sensor Nano into LabVIEW via an NI MyRIO (1900). I was able to set up a sequence that communicates with the sensor through SPI. However, every time I run the program, it just spits out a single value of 255.
I think understand that I need to include something that allows all the bytes to be read. I just don't know how to go about it.
As an example, I'm trying to read the gyros (0x26) which have a return length of 12 and is a vector (float x3).
Here is my labview code
and here is the manual for the sensor. The commands I'm using are on pages 29-33. In the image, 0x2B is 'read temperature'.
Any help would be greatly appreciated! Thanks :)
Edit: i had messed up the wiring so now the output jumps between ~35 to 255. I'm still having trouble getting all 3 gyro values from the SPI read.
Quoting from Joe Friedrichsen in his comment:
The express block that resets the sensor is not guaranteed to precede the loop because there is no data flow between them. The LabVIEW runtime can see two independent and parallel groups and may choose to execute them simultaneously (which on the wire might mean reset comes between loop commands) or in "reverse" order. Add a wire from reset block to create a terminal on the loop.
Here's a picture of the fix.
You may wish to consider stringing the error wire through your program and wiring it to the stop terminal of the While Loop. Currently, your loop will keep running even if there's a fault in your hardware. Using the error wire would eliminate the need for the flat sequence structure.

How to prevent a program on TI-84 from being transferred to another calculator?

I am trying to allow others to use my programs, but I don't want those users to be sharing the programs without my permission, so my goal is to prevent the users from doing this on the TI-84 calculator, but I have had no apparent luck.
For the TI-84 calculator, I have tried the getkey command and the stop. I have tried conditional statements with for and while loops, but I can't seem to prevent the user from sharing the code.
Prompt V
V+2 -> C
Disp C
The code works like it is intended but I can't prevent people from transferring this code to other calculators.
There is no way to prevent people from transferring programs over the calculator. What you could try to do is make it so that after you transfer the program yourself you store a password into a variable that they are unlikely to use (done manually, make sure to clear afterwards). When the program runs it checks if the variable stores the correct value. If someone else transfers the program and doesn't load the password into the correct variable, then the program won't work. However, they can figure this out by simply reading the code. If you want to be extra tricky, you can create a simple hashing algorithm and read from multiple variables. For instance, storing 123 in Z could be hashed by *28 + 54 mod 71 to get to 19. Apply those math functions to the chosen variables and then compare to their hashed value. This protects people from figuring out the password by checking the program (since 19 cannot be reversed to 123). Of course, they can just delete the check, so try to hide it somewhere among junk code. Now, they might overwrite those variables where your password is stored (often by running other programs that use those variables). Simply tell them that they have to come back to you so you can "fix" their program. Hope this helps.

Could anyone please explain what is c000 means in c000.snappy.parquet or c000.snappy.orc??

I have searched through every documentation and still didn't find why there is a prefix and what is c000 in the below file naming convention:
file:/Users/stephen/p/spark/f1/part-00000-445036f9-7a40-4333-8405-8451faa44319-
c000.snappy.parquet
You should use "Talk is cheap, show me the code." methodology. Everything is not documented and one way to go is just the code.
Consider part-1-2_3-4.parquet :
Split/Partition number.
Random UUID to prevent collision between different (appending) write jobs.
Unique Job/Task ID (sometimes it will not be included).
The "c" stands for count. This is file counter which means the number of files that have been written in the past for this specific partition. This is used to limit the max number of records written for a single file. The value should start from 0.
I found it based on this code and this code.

COMPARE AND WRITE on umapped block?

The COMPARE AND WRITE command description in the SBC-4 doesn't say anything about the case when the range of logical blocks to be replaced contains unmapped blocks.
What's the common practice to deal with this case on the target side? Should a target assume that the verification step is always successful in the case when an initiator asks to replace unmapped blocks with something meaningful?
When a block is unmapped it is equal to all zeroes. You should perform all your commands that read or compare data with this block as it was just zeroes.

is the code for interpreted languages re-interpreted every time the line is reached?

suppose that no bytecode is generated for a program, like in Ruby, Perl, or PHP, in this case, is line 1 below re-interpreted every time the execution reach line 1 again?
while ($indexArrayMoviesData < $countArrayMoviesData + $countNewlyAddedMoviesData) {
# do something
}
that is, if the loop runs 100,000 times, then that line will be re-interpreted 100,000 times?
and if so, the bytecode creation helps not only the initial start up of he program, but also during the execution? (because code doesn't need to be re-interpreted again)
Typically, it'll be converted into byte code and that byte code will then be executed.
But in the case of PHP for example, the byte code is regenerated on every request/page view. Unless you install a byte code (or opcode as it's called often in the case of PHP) cache, such as XCache, APC or EAccelerator.
For recent languages, including perl, the code is precompiled before being executed. So most of the analysis work is performed only once.
This is not the case for shells, which interpret every line each time they execute them.
If the interpreter is sensible it would hopefully check if $countArrayMoviesData or $countNewlyAddedMoviesData were altered during the loop and if they weren't then the sum could be calculated and kept.
If the values are updated within the loop then in all likelihood even the bytecode would require an addition operation to take place, not making it any more efficient.
Very, very few interpreters will do this. An example is the ages-old, no longer used Hypertalk interpreter for Hypercard where you could actually rewrite the text of your code programatically (it's just a string!)
Even interpreters that don't produce byte code will parse your code first, as it's hard to do that line by line and much easier to do it all at once. So a really simple interpreter will basically have a tree, with a node for the "where" loop with two children: one "less than" expression for the conditional, and one block for the body of the loop.
The answer to your question, as all consultants know, is "it depends."
You're right, in some interpreted languages, that line may be reinterpreted each time. I suspect most shells handle it roughly this way.
The original versions of Basic also did it this way.
Most current interpreters will at least tokenize the language, so that the text doesn't need to be re-scanned each time. That is, a BASIC-ish program like
00010 LET A=42
00020 DO WHILE A > 0
00025 LET A = A - 1
00030 ENDDO
would convert it at the least to small tokens for the keywords, and addresses for the variable, something like
LET $0003, 42
LABEL 00020
LETEST A, 0
IFTRUEGOTO 00030
SUB $0005, $0003, 1
GOTO 00020
LABEL 00030
where each word in upper case in the translation is a single integer internally. That way, there's a single lexical analysis pass to translate it, followed by the interpreter being able to just interpret the token values.
Of course, once you go that far, you find yourself thinking "gee, why not use real opcodes?"

Resources