Rounding decimals matlab manipulates

I need to write, in 1 code line, given a number (pos, neg, int, float), how do I round it without preset functions like fix, round, etc.?

9 comentarios

Walter Roberson
Walter Roberson el 31 de Jul. de 2017
Editada: Walter Roberson el 31 de Jul. de 2017
How would you do it in multiple lines of code?
hint 1: watch out for negative
hint 2: log10 to determine the number of decimal places it already has
hint 3: watch out for 0
hint 4: watch out for integers that are exact powers of 10: for example, log10(100) = 2 exactly, log10(99.999) = slightly less than 2.
hint 5: x * (1-eps) for positive x is always less than x
Stephen23
Stephen23 el 1 de Ag. de 2017
yuval ohayon's "Answer" moved here:
Well thank for the hints Accualy in one code line zerp loops. Smone can help?
Guillaume
Guillaume el 1 de Ag. de 2017
@Yuval,
a) Use Comment on this question rather than starting an answer
b) proofread what you read. The above is incomprehensible.
Jan
Jan el 1 de Ag. de 2017
Editada: Jan el 1 de Ag. de 2017
@yuval ohayon: "without the preset function like fix,round" is not clear enough. Is "rem" and "mod" allowed or not? If so, simply use "rem(..., 1)".
Walter Roberson
Walter Roberson el 1 de Ag. de 2017
Is floor() permitted?
yuval ohayon
yuval ohayon el 1 de Ag. de 2017
Im not familier with rem function,floor isn permitted. For my understanding it must be with mod func,but how i find if the numbet is upper .5 or down .5
yuval ohayon
yuval ohayon el 1 de Ag. de 2017
Someones help please
Steven Lord
Steven Lord el 2 de Ag. de 2017
In general people on MATLAB Answers don't post answers to homework questions or questions that sound like homework. If you show what you've done to try to solve the problem on your own and ask a specific question about where you're having difficulty you may receive some more specific suggestions.
Of course, if this isn't a homework problem, just use the round function or the other rounding functions included in MATLAB.
Image Analyst
Image Analyst el 2 de Ag. de 2017
mod() is also a built in function like round(), fix(), int32(), ceil(), floor(), etc.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 1 de Ag. de 2017
Try it:
x = 13.3
y = mod(x, 1)
z = 13.6
y = mod(x, 1)
Now you should see how to use the output to decide, if the rest of the division by 1 is lower or higher than 0.5.

3 comentarios

yuval ohayon
yuval ohayon el 1 de Ag. de 2017
Well ,how i make condision with no loop x=input number Y=x-xmod(x,1)
Help
Walter Roberson
Walter Roberson el 1 de Ag. de 2017
x - mod(x,1) + mod(x,1)>0.5
Jan
Jan el 2 de Ag. de 2017
int64() rounds also. And you can use cast() to preserve the original class.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 31 de Jul. de 2017

Comentada:

el 2 de Ag. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by