Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:13 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Sorry for the stupid math question

Author Message
RCMAN
Newbie

Joined: 31/10/2020
Location: Canada
Posts: 29
Posted: 03:27pm 10 Dec 2020
Copy link to clipboard 
Print this post

If I have a float value how do I get the value and remainder separately?

Thanks
RC
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 03:34pm 10 Dec 2020
Copy link to clipboard 
Print this post

Hi RC,

try FIX(number)

Michael
causality ≠ correlation ≠ coincidence
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 03:36pm 10 Dec 2020
Copy link to clipboard 
Print this post

x= 3.141

y=Int(x)' integer part
z=x-y ' fractional part
 
RCMAN
Newbie

Joined: 31/10/2020
Location: Canada
Posts: 29
Posted: 04:01pm 10 Dec 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  x= 3.141

y=Int(x)' integer part
z=x-y ' fractional part



Thank you for both your answers.

RC
 
RCMAN
Newbie

Joined: 31/10/2020
Location: Canada
Posts: 29
Posted: 04:49pm 10 Dec 2020
Copy link to clipboard 
Print this post


  RCMAN said  
  CaptainBoing said  x= 3.141

y=Int(x)' integer part
z=x-y ' fractional part



Thank you for both your answers.

RC


Working Perfect!
Edited 2020-12-11 02:50 by RCMAN
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 08:18pm 10 Dec 2020
Copy link to clipboard 
Print this post

Why not use MOD?

eg mod(3.141,1) = .141
Rob White
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 08:22pm 10 Dec 2020
Copy link to clipboard 
Print this post

I wonder which way is less computationally complex:

Int(x) and Mod(x,1)   OR

Int(x) and calculate (x-int)

Any other ways?

Nim
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 08:24pm 10 Dec 2020
Copy link to clipboard 
Print this post

  Nimue said  I wonder which way is less computationally complex:

Int(x) and Mod(x,1)   OR

Int(x) and calculate (x-int)

Any other ways?

Nim


I rejected making into a string and splitting up and then making back into numbers.
Entropy is not what it used to be
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 08:26pm 10 Dec 2020
Copy link to clipboard 
Print this post

  Nimue said  I wonder which way is less computationally complex:

Int(x) and Mod(x,1)   OR

Int(x) and calculate (x-int)

Any other ways?

Nim


I don't know about MMBASIC but I do know that in some BASICs, the subtraction method sometimes has a rounding error. In ANSI/ISO BASIC, there's an FP() function to get the fractional part that eliminates errors.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 08:27pm 10 Dec 2020
Copy link to clipboard 
Print this post

  Bizzie said  Why not use MOD?

eg mod(3.141,1) = .141


Clever!
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 08:37pm 10 Dec 2020
Copy link to clipboard 
Print this post

  Nimue said  I rejected making into a string and splitting up and then making back into numbers.

I don't!

>Print field§("3.14159",1,".")
3
>Print field§("3.14159",2,".")
14159

not soo bad!
causality ≠ correlation ≠ coincidence
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 08:44pm 10 Dec 2020
Copy link to clipboard 
Print this post

  Quote  mod(3.141,1)


Have you tried?

The format for MOD would be 3.141 MOD 1 in MMBasic
and MOD seems to only works on integers so will always give zero.

It would be worth checking the difference between CINT, INT and FIX if you are likely to have negative numbers.
Anther can of worms.

PRINT "N",,"INT(N)",,"CINT(N)",,"FIX(N)"
FOR n = -2 TO 2 STEP 0.25
PRINT n,,INT(n),,CINT(n),,FIX(n)
NEXT n


Jim
VK7JH
MMedit
 
RCMAN
Newbie

Joined: 31/10/2020
Location: Canada
Posts: 29
Posted: 08:44pm 10 Dec 2020
Copy link to clipboard 
Print this post

  twofingers said  
  Nimue said  I rejected making into a string and splitting up and then making back into numbers.

I don't!

>Print field§("3.14159",1,".")
3
>Print field§("3.14159",2,".")
14159

not soo bad!



I was looking for a modulus command used in C

RC
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 08:53pm 10 Dec 2020
Copy link to clipboard 
Print this post

INT reduces the number by the fractional part - potentially problematic for negative numbers.
CINT rounds to the nearest integer - up or down.
FIX just hacks off the fractional part - the positive or negative value gets closer to zero.

Take your pick...
Visit Vegipete's *Mite Library for cool programs.
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 09:09pm 10 Dec 2020
Copy link to clipboard 
Print this post

@RC

  Quote  I was looking for a modulus command used in C


something like that:

function fract(a as float) as float
 fract=a-FIX(a)
end function

causality ≠ correlation ≠ coincidence
 
RCMAN
Newbie

Joined: 31/10/2020
Location: Canada
Posts: 29
Posted: 09:17pm 10 Dec 2020
Copy link to clipboard 
Print this post

  twofingers said  @RC

  Quote  I was looking for a modulus command used in C


something like that:

function fract(a as float) as float
 fract=a-FIX(a)
end function



I was working out row and column and CaptainBoing example helped me solve that problem.

It's all good now.

One thing out of the way, moving on to animation and background detection.

RC
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025